2 * Copyright (C) 2010 Nokia Corporation.
4 * Author: Zeeshan Ali (Khattak) <zeeshan.ali@nokia.com>
5 * <zeeshanak@gnome.org>
7 * This file is part of Rygel.
9 * Rygel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Rygel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 private errordomain Rygel.TestError {
28 private class Rygel.HTTPIdentityHandler : GLib.Object {}
30 private class Rygel.MediaItem : GLib.Object {
31 public int64 size = 2048;
34 private class Rygel.Thumbnail : GLib.Object {
35 public int64 size = 1024;
38 private class Rygel.Subtitle : GLib.Object {
39 public int64 size = 512;
42 private class Rygel.HTTPGet : GLib.Object {
43 public const string ITEM_URI = "http://DoesntMatterWhatThisIs";
45 public Soup.Message msg;
46 public MediaItem item;
47 public Thumbnail thumbnail;
48 public Subtitle subtitle;
50 public HTTPIdentityHandler handler;
52 public HTTPGet (Thumbnail? thumbnail, Subtitle? subtitle) {
53 this.msg = new Soup.Message ("HTTP", ITEM_URI);
54 this.item = new MediaItem ();
55 this.handler = new HTTPIdentityHandler ();
56 this.thumbnail = thumbnail;
57 this.subtitle = subtitle;
60 public HTTPGet.seek_start (int64 start,
63 this (thumbnail, subtitle);
65 this.add_headers (start, -1);
68 public HTTPGet.seek_stop (int64 stop,
71 this (thumbnail, subtitle);
73 this.add_headers (0, stop);
76 public HTTPGet.seek_start_stop (int64 start,
80 this (thumbnail, subtitle);
82 this.add_headers (start, stop);
85 private void add_headers (int64 start, int64 stop) {
86 this.msg.request_headers.set_range (start, stop);
90 private class Rygel.HTTPByteSeekTest : GLib.Object {
91 private Regex range_regex;
93 public static int main (string[] args) {
95 var test = new HTTPByteSeekTest ();
98 } catch (TestError.SKIP error) {
100 } catch (Error error) {
101 critical ("%s", error.message);
109 public void run () throws HTTPSeekError {
110 var thumbnails = new Thumbnail[] { null, new Thumbnail () };
111 var subtitles = new Subtitle[] { null, new Subtitle () };
113 foreach (var thumbnail in thumbnails) {
114 foreach (var subtitle in subtitles) {
115 this.test_no_seek (thumbnail, subtitle);
116 this.test_start_only_seek (thumbnail, subtitle);
117 this.test_stop_only_seek (thumbnail, subtitle);
118 this.test_start_stop_seek (thumbnail, subtitle);
123 private HTTPByteSeekTest () {
124 this.range_regex = new Regex ("bytes +[0-9]+-[0-9]+/[0-9]+",
125 RegexCompileFlags.CASELESS);
128 private void test_no_seek (Thumbnail? thumbnail,
129 Subtitle? subtitle) throws HTTPSeekError {
130 var request = new HTTPGet (thumbnail, subtitle);
133 if (request.thumbnail != null) {
134 size = request.thumbnail.size;
135 } else if (request.subtitle != null) {
136 size = request.subtitle.size;
138 size = request.item.size;
141 this.test_seek (request, 0, size - 1);
144 private void test_start_only_seek (Thumbnail? thumbnail,
146 throws HTTPSeekError {
147 var request = new HTTPGet.seek_start (128, thumbnail, subtitle);
150 if (request.thumbnail != null) {
151 size = request.thumbnail.size;
152 } else if (request.subtitle != null) {
153 size = request.subtitle.size;
155 size = request.item.size;
158 this.test_seek (request, 128, size - 1);
161 private void test_stop_only_seek (Thumbnail? thumbnail,
163 throws HTTPSeekError {
164 var request = new HTTPGet.seek_stop (128, thumbnail, subtitle);
166 this.test_seek (request, 0, 128);
169 private void test_start_stop_seek (Thumbnail? thumbnail,
171 throws HTTPSeekError {
172 var request = new HTTPGet.seek_start_stop (128,
177 this.test_seek (request, 128, 256);
180 private void test_seek (HTTPGet request,
182 int64 stop) throws HTTPSeekError {
183 assert (HTTPByteSeek.needed (request));
185 var seek = new HTTPByteSeek (request);
186 seek.add_response_headers ();
188 assert (seek != null);
189 assert (seek.start == start);
190 assert (seek.stop == stop);
192 if (request.thumbnail != null) {
193 assert (seek.total_length == request.thumbnail.size);
194 } else if (request.subtitle != null) {
195 assert (seek.total_length == request.subtitle.size);
197 assert (seek.total_length == request.item.size);
200 var header = request.msg.response_headers.get ("Accept-Ranges");
201 assert (header == "bytes");
202 header = request.msg.response_headers.get ("Content-Range");
203 assert (header != null);
204 assert (this.range_regex.match (header));
206 assert (request.msg.response_headers.get_content_length () ==