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 public class Rygel.MediaObject : GLib.Object {
31 public int64 size = 2048;
34 private class Rygel.MediaItem : MediaObject {
37 private class Rygel.Thumbnail : GLib.Object {
38 public int64 size = 1024;
41 private class Rygel.Subtitle : GLib.Object {
42 public int64 size = 512;
45 public class Rygel.MediaContainer : MediaObject {
48 public class Rygel.ClientHacks : GLib.Object {
49 public static ClientHacks create (Soup.Message msg) throws Error {
50 return new ClientHacks ();
53 public bool force_seek () {
58 private class Rygel.HTTPGet : GLib.Object {
59 public const string ITEM_URI = "http://DoesntMatterWhatThisIs";
61 public Soup.Message msg;
62 public MediaObject object;
63 public Thumbnail thumbnail;
64 public Subtitle subtitle;
66 public HTTPIdentityHandler handler;
68 public HTTPGet (Thumbnail? thumbnail, Subtitle? subtitle) {
69 this.msg = new Soup.Message ("HTTP", ITEM_URI);
70 this.object = new MediaItem ();
71 this.handler = new HTTPIdentityHandler ();
72 this.thumbnail = thumbnail;
73 this.subtitle = subtitle;
76 public HTTPGet.seek_start (int64 start,
79 this (thumbnail, subtitle);
81 this.add_headers (start, -1);
84 public HTTPGet.seek_stop (int64 stop,
87 this (thumbnail, subtitle);
89 this.add_headers (0, stop);
92 public HTTPGet.seek_start_stop (int64 start,
96 this (thumbnail, subtitle);
98 this.add_headers (start, stop);
101 public HTTPGet.inverted_range () {
103 this.msg.request_headers.append ("Range", "bytes=34-0");
106 public HTTPGet.invalid_range () {
108 this.msg.request_headers.append ("Range", "bytes=a-b");
111 private void add_headers (int64 start, int64 stop) {
112 this.msg.request_headers.set_range (start, stop);
116 private class Rygel.HTTPByteSeekTest : GLib.Object {
117 private Regex range_regex;
119 public static int main (string[] args) {
121 var test = new HTTPByteSeekTest ();
124 /* TODO: Nothing throws this exception. Should it?
125 } catch (TestError.SKIP error) {
128 } catch (Error error) {
129 critical ("%s", error.message);
137 public void run () throws HTTPSeekError {
138 var thumbnails = new Thumbnail[] { null, new Thumbnail () };
139 var subtitles = new Subtitle[] { null, new Subtitle () };
141 foreach (var thumbnail in thumbnails) {
142 foreach (var subtitle in subtitles) {
143 this.test_no_seek (thumbnail, subtitle);
144 this.test_start_only_seek (thumbnail, subtitle);
145 this.test_stop_only_seek (thumbnail, subtitle);
146 this.test_start_stop_seek (thumbnail, subtitle);
151 new HTTPByteSeek (new HTTPGet.inverted_range ());
152 assert_not_reached ();
153 } catch (HTTPSeekError error) {}
156 new HTTPByteSeek (new HTTPGet.invalid_range ());
157 assert_not_reached ();
158 } catch (HTTPSeekError error) {}
161 private HTTPByteSeekTest () {
163 this.range_regex = new Regex ("bytes +[0-9]+-[0-9]+/[0-9]+",
164 RegexCompileFlags.CASELESS);
165 } catch (RegexError error) {
166 // This means that it is not a regular expression
167 assert_not_reached ();
171 private void test_no_seek (Thumbnail? thumbnail,
172 Subtitle? subtitle) throws HTTPSeekError {
173 var request = new HTTPGet (thumbnail, subtitle);
176 if (request.thumbnail != null) {
177 size = request.thumbnail.size;
178 } else if (request.subtitle != null) {
179 size = request.subtitle.size;
181 size = request.object.size;
184 this.test_seek (request, 0, size - 1);
187 private void test_start_only_seek (Thumbnail? thumbnail,
189 throws HTTPSeekError {
190 var request = new HTTPGet.seek_start (128, thumbnail, subtitle);
193 if (request.thumbnail != null) {
194 size = request.thumbnail.size;
195 } else if (request.subtitle != null) {
196 size = request.subtitle.size;
198 size = request.object.size;
201 this.test_seek (request, 128, size - 1);
204 private void test_stop_only_seek (Thumbnail? thumbnail,
206 throws HTTPSeekError {
207 var request = new HTTPGet.seek_stop (128, thumbnail, subtitle);
209 this.test_seek (request, 0, 128);
212 private void test_start_stop_seek (Thumbnail? thumbnail,
214 throws HTTPSeekError {
215 var request = new HTTPGet.seek_start_stop (128,
220 this.test_seek (request, 128, 256);
223 private void test_seek (HTTPGet request,
225 int64 stop) throws HTTPSeekError {
226 assert (HTTPByteSeek.needed (request));
228 var seek = new HTTPByteSeek (request);
229 seek.add_response_headers ();
231 assert (seek != null);
232 assert (seek.start == start);
233 assert (seek.stop == stop);
235 if (request.thumbnail != null) {
236 assert (seek.total_length == request.thumbnail.size);
237 } else if (request.subtitle != null) {
238 assert (seek.total_length == request.subtitle.size);
240 assert (seek.total_length == request.object.size);
243 if (request.msg.request_headers.get_one ("Range") != null) {
244 var header = request.msg.response_headers.get_one ("Accept-Ranges");
245 assert (header == "bytes");
246 header = request.msg.response_headers.get_one ("Content-Range");
247 assert (header != null);
248 assert (this.range_regex.match (header));
251 assert (request.msg.response_headers.get_content_length () ==
254 /* TODO: This is just here to avoid a warning about
255 * requested() not being used.
256 * How should this really be tested?
257 * Sometimes the result here is true, and sometimes it is false.
259 /* bool result = */ HTTPByteSeek.requested(request);