tests: Port to vala 0.11.1
[profile/ivi/rygel.git] / tests / rygel-http-post-test.vala
1 /*
2  * Copyright (C) 2010 Nokia Corporation.
3  *
4  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
5  *                               <zeeshan.ali@nokia.com>
6  *
7  * This file is part of Rygel.
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 using Soup;
25 using Gee;
26
27 public errordomain Rygel.TestError {
28     SKIP = 77,
29     TIMEOUT
30 }
31
32 public class Rygel.HTTPPostTest : GLib.Object {
33     protected HTTPServer server;
34     protected HTTPClient client;
35
36     private bool server_done;
37     private bool client_done;
38
39     private MainLoop main_loop;
40
41     private Error error;
42
43     public static int main (string[] args) {
44         try {
45             var test = new HTTPPostTest ();
46
47             test.run ();
48         } catch (TestError.SKIP error) {
49             return error.code;
50         } catch (Error error) {
51             critical ("%s", error.message);
52
53             return -1;
54         }
55
56         return 0;
57     }
58
59     public HTTPPostTest () throws Error {
60         this.server = new HTTPServer ();
61         this.client = new HTTPClient (this.server.context,
62                                       this.server.uri);
63         this.main_loop = new MainLoop (null, false);
64     }
65
66     public virtual void run () throws Error {
67         Timeout.add_seconds (3, this.on_timeout);
68         this.server.message_received.connect (this.on_message_received);
69         this.client.completed.connect (this.on_client_completed);
70
71         this.client.run.begin ();
72
73         this.main_loop.run ();
74
75         if (this.error != null) {
76             throw this.error;
77         }
78     }
79
80     private HTTPRequest create_request (Soup.Message msg) throws Error {
81         return new HTTPPost (this.server,
82                             this.server.context.server,
83                             msg);
84     }
85
86     private void on_client_completed (StateMachine client) {
87         this.client_done = true;
88         this.check_and_exit.begin ();
89     }
90
91     private void on_message_received (HTTPServer   server,
92                                       Soup.Message msg) {
93         this.handle_client_message.begin (msg);
94     }
95
96     private async void handle_client_message (Soup.Message msg) {
97         try {
98             var request = this.create_request (msg);
99
100             yield request.run ();
101
102             assert ((request as HTTPPost).item != null);
103
104             this.server_done = true;
105             this.check_and_exit.begin ();
106         } catch (Error error) {
107             this.error = error;
108             this.main_loop.quit ();
109
110             return;
111         }
112     }
113
114     private bool on_timeout () {
115         this.error = new TestError.TIMEOUT ("Timeout");
116         this.main_loop.quit ();
117
118         return false;
119     }
120
121     private async void check_and_exit () {
122         if (!(this.server_done && this.client_done)) {
123             return;
124         }
125
126         try {
127             var file = this.server.root_container.item.file;
128             var stream = yield file.read_async (Priority.HIGH, null);
129             var buffer = new uint8[HTTPClient.LENGTH];
130
131             yield stream.read_async (buffer, Priority.HIGH, null);
132
133             for (var i = 0; i < HTTPClient.LENGTH; i++) {
134                 assert (buffer[i] == this.client.content[i]);
135             }
136         } catch (Error error) {
137             this.error = error;
138         }
139
140         this.main_loop.quit ();
141     }
142 }
143
144 public class Rygel.HTTPServer : GLib.Object {
145     private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
146     public string path_root {
147         get {
148             return SERVER_PATH;
149         }
150     }
151
152     public MediaContainer root_container;
153     public GUPnP.Context context;
154
155     public string uri {
156         owned get {
157             var item_uri = new HTTPItemURI (this.root_container.ITEM_ID,
158                                             this);
159
160             return item_uri.to_string ();
161         }
162     }
163
164     public signal void message_received (Soup.Message message);
165
166     public HTTPServer () throws TestError {
167         try {
168             this.context = new GUPnP.Context (null, "lo", 0);
169         } catch (Error error) {
170             throw new TestError.SKIP ("Network context not available");
171         }
172
173         assert (this.context != null);
174         assert (this.context.host_ip != null);
175         assert (this.context.port > 0);
176
177         context.server.request_started.connect (this.on_request_started);
178
179         this.root_container = new MediaContainer ();
180     }
181
182     private void on_request_started (Soup.Server        server,
183                                      Soup.Message       msg,
184                                      Soup.ClientContext client) {
185         msg.got_headers.connect (this.on_got_headers);
186     }
187
188     private void on_got_headers (Soup.Message msg) {
189         this.message_received (msg);
190     }
191 }
192
193 public class Rygel.HTTPClient : GLib.Object, StateMachine {
194     public const size_t LENGTH = 1024;
195
196     public uint8[] content;
197
198     public GUPnP.Context context;
199     public Soup.Message msg;
200
201     public Cancellable cancellable { get; set; }
202
203     public HTTPClient (GUPnP.Context context,
204                        string        uri) {
205         this.context = context;
206         this.content = new uint8[1024];
207
208         this.msg = new Soup.Message ("POST",  uri);
209         assert (this.msg != null);
210     }
211
212     public async void run () {
213         SourceFunc run_continue = run.callback;
214
215         this.msg.request_body.append (MemoryUse.COPY, content);
216
217         this.context.session.queue_message (this.msg, (session, msg) => {
218             run_continue ();
219         });
220
221         yield;
222
223         this.completed ();
224     }
225 }
226
227 public class Rygel.MediaContainer : Rygel.MediaObject {
228     public const string ITEM_ID = "TestItem";
229
230     public MediaItem item;
231
232     public MediaContainer () {
233         this.item = new MediaItem (ITEM_ID);
234     }
235
236     public async MediaObject? find_object (string       item_id,
237                                            Cancellable? cancellable)
238                                            throws Error {
239         SourceFunc find_object_continue = find_object.callback;
240         Idle.add (() => {
241             find_object_continue ();
242
243             return false;
244         });
245
246         yield;
247
248         if (item_id == ITEM_ID) {
249             return this.item;
250         } else {
251             return null;
252         }
253     }
254 }
255
256 public class Rygel.MediaItem : Rygel.MediaObject {
257     public const string URI = "file:///tmp/rygel-upload-test.wav";
258
259     public string id;
260     public long size = 1024;
261     public long duration = 1024;
262
263     public File file;
264
265     public MediaItem (string id) {
266         this.id = id;
267
268         this.file = File.new_for_uri (URI);
269         try {
270             this.file.replace (null, false, 0, null);
271         } catch (IOError.EXISTS error) {
272         } catch (GLib.Error error) {
273             assert_not_reached ();
274         }
275     }
276
277     ~MediaItem() {
278         try {
279             this.file.delete (null);
280         } catch (GLib.Error error) {
281             assert_not_reached ();
282         }
283     }
284
285     public async File? get_writable (Cancellable? cancellable) throws Error {
286         SourceFunc get_writable_continue = get_writable.callback;
287
288         Idle.add (() => {
289             get_writable_continue ();
290
291             return false;
292         });
293
294         yield;
295
296         return this.file;
297     }
298 }
299
300 internal class Rygel.HTTPResponse : Rygel.StateMachine, GLib.Object {
301     public Cancellable cancellable { get; set; }
302
303     private Soup.Message msg;
304     private Soup.Server server;
305
306     public HTTPResponse (HTTPPost get_request) {
307         this.msg = get_request.msg;
308         this.server = get_request.server;
309     }
310
311     public async void run () {
312         SourceFunc run_continue = run.callback;
313
314         Idle.add (() => {
315             run_continue ();
316
317             return false;
318         });
319
320         yield;
321
322         this.msg.set_status (Soup.KnownStatusCode.OK);
323         this.server.unpause_message (msg);
324
325         this.completed ();
326     }
327 }
328
329 public class Rygel.MediaObject {}