2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
3 * 2008 Pekka Lampila <pekka.lampila@iki.fi>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
27 #include "swfdec_test_http_request.h"
28 #include "swfdec_test_http_server.h"
29 #include "swfdec_test_buffer.h"
30 #include "swfdec_test_function.h"
31 #include "swfdec_test_utils.h"
34 swfdec_test_http_request_new (SwfdecAsContext
*context
,
35 SwfdecTestHTTPServer
*server
, SoupMessage
*message
)
40 g_return_val_if_fail (SWFDEC_IS_AS_CONTEXT (context
), NULL
);
41 g_return_val_if_fail (SWFDEC_IS_TEST_HTTP_SERVER (server
), NULL
);
42 g_return_val_if_fail (SOUP_IS_MESSAGE (message
), NULL
);
44 if (!swfdec_as_context_use_mem (context
, sizeof (SwfdecTestHTTPRequest
)))
47 ret
= g_object_new (SWFDEC_TYPE_TEST_HTTP_REQUEST
, NULL
);
48 swfdec_as_object_add (ret
, context
, sizeof (SwfdecTestHTTPRequest
));
49 swfdec_as_object_get_variable (context
->global
,
50 swfdec_as_context_get_string (context
, "HTTPRequest"), &val
);
52 if (SWFDEC_AS_VALUE_IS_OBJECT (&val
))
53 swfdec_as_object_set_constructor (ret
, SWFDEC_AS_VALUE_GET_OBJECT (&val
));
55 soup_message_headers_set_encoding (message
->response_headers
,
56 SOUP_ENCODING_CHUNKED
);
57 soup_message_set_flags (message
, SOUP_MESSAGE_OVERWRITE_CHUNKS
);
58 soup_message_set_status (message
, SOUP_STATUS_OK
);
60 SWFDEC_TEST_HTTP_REQUEST (ret
)->server
= server
;
61 SWFDEC_TEST_HTTP_REQUEST (ret
)->message
= message
;
66 /*** SWFDEC_TEST_HTTP_REQUEST ***/
68 G_DEFINE_TYPE (SwfdecTestHTTPRequest
, swfdec_test_http_request
, SWFDEC_TYPE_AS_OBJECT
)
71 swfdec_test_http_request_dispose (GObject
*object
)
73 SwfdecTestHTTPRequest
*request
= SWFDEC_TEST_HTTP_REQUEST (object
);
75 g_object_unref (request
->message
);
76 request
->message
= NULL
;
78 G_OBJECT_CLASS (swfdec_test_http_request_parent_class
)->dispose (object
);
82 swfdec_test_http_request_mark (SwfdecAsObject
*object
)
84 SwfdecTestHTTPRequest
*request
= SWFDEC_TEST_HTTP_REQUEST (object
);
86 swfdec_as_object_mark (SWFDEC_AS_OBJECT (request
->server
));
87 swfdec_as_object_mark (SWFDEC_AS_OBJECT (request
->headers
));
89 SWFDEC_AS_OBJECT_CLASS (swfdec_test_http_request_parent_class
)->mark (object
);
93 swfdec_test_http_request_class_init (SwfdecTestHTTPRequestClass
*klass
)
95 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
96 SwfdecAsObjectClass
*asobject_class
= SWFDEC_AS_OBJECT_CLASS (klass
);
98 object_class
->dispose
= swfdec_test_http_request_dispose
;
99 asobject_class
->mark
= swfdec_test_http_request_mark
;
103 swfdec_test_http_request_init (SwfdecTestHTTPRequest
*this)
109 SWFDEC_TEST_FUNCTION ("HTTPRequest_toString", swfdec_test_http_request_toString
, 0)
111 swfdec_test_http_request_toString (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
112 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
114 SwfdecTestHTTPRequest
*request
;
118 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "");
120 string
= g_string_new (request
->message
->method
);
121 string
= g_string_append (string
, " ");
123 uri
= soup_message_get_uri (request
->message
);
124 string
= g_string_append (string
, soup_uri_to_string (uri
, FALSE
));
126 SWFDEC_AS_VALUE_SET_STRING (retval
,
127 swfdec_as_context_give_string (cx
, g_string_free (string
, FALSE
)));
130 SWFDEC_TEST_FUNCTION ("HTTPRequest_get_server", swfdec_test_http_request_get_server
, 0)
132 swfdec_test_http_request_get_server (SwfdecAsContext
*cx
,
133 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
134 SwfdecAsValue
*retval
)
136 SwfdecTestHTTPRequest
*request
;
138 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "");
140 SWFDEC_AS_VALUE_SET_OBJECT (retval
, SWFDEC_AS_OBJECT (request
->server
));
143 SWFDEC_TEST_FUNCTION ("HTTPRequest_get_url", swfdec_test_http_request_get_url
, 0)
145 swfdec_test_http_request_get_url (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
146 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
148 SwfdecTestHTTPRequest
*request
;
151 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "");
153 uri
= soup_message_get_uri (request
->message
);
155 SWFDEC_AS_VALUE_SET_STRING (retval
,
156 swfdec_as_context_give_string (cx
, soup_uri_to_string (uri
, FALSE
)));
159 SWFDEC_TEST_FUNCTION ("HTTPRequest_get_path", swfdec_test_http_request_get_path
, 0)
161 swfdec_test_http_request_get_path (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
162 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
164 SwfdecTestHTTPRequest
*request
;
167 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "");
169 uri
= soup_message_get_uri (request
->message
);
171 SWFDEC_AS_VALUE_SET_STRING (retval
,
172 swfdec_as_context_give_string (cx
, soup_uri_to_string (uri
, TRUE
)));
176 swfdec_test_http_request_foreach_set_headers (const char *name
,
177 const char *value
, gpointer user_data
)
179 SwfdecTestHTTPRequest
*request
= user_data
;
183 g_return_if_fail (SWFDEC_IS_AS_OBJECT (request
->headers
));
185 cx
= request
->headers
->context
;
187 SWFDEC_AS_VALUE_SET_STRING (&val
, swfdec_as_context_get_string (cx
, value
));
188 swfdec_as_object_set_variable (request
->headers
,
189 swfdec_as_context_get_string (cx
, name
), &val
);
192 SWFDEC_TEST_FUNCTION ("HTTPRequest_get_headers", swfdec_test_http_request_get_headers
, 0)
194 swfdec_test_http_request_get_headers (SwfdecAsContext
*cx
,
195 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
196 SwfdecAsValue
*retval
)
198 SwfdecTestHTTPRequest
*request
;
200 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "");
202 if (!request
->headers
) {
203 request
->headers
= swfdec_as_object_new_empty (cx
);
204 soup_message_headers_foreach (request
->message
->request_headers
,
205 swfdec_test_http_request_foreach_set_headers
, request
);
208 SWFDEC_AS_VALUE_SET_OBJECT (retval
, request
->headers
);
211 SWFDEC_TEST_FUNCTION ("HTTPRequest_get_contentType", swfdec_test_http_request_get_contentType
, 0)
213 swfdec_test_http_request_get_contentType (SwfdecAsContext
*cx
,
214 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
215 SwfdecAsValue
*retval
)
217 SwfdecTestHTTPRequest
*request
;
220 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "");
222 value
= soup_message_headers_get (request
->message
->response_headers
,
226 SWFDEC_AS_VALUE_SET_STRING (retval
,
227 swfdec_as_context_get_string (cx
, value
));
229 SWFDEC_AS_VALUE_SET_NULL (retval
);
233 SWFDEC_TEST_FUNCTION ("HTTPRequest_set_contentType", swfdec_test_http_request_set_contentType
, 0)
235 swfdec_test_http_request_set_contentType (SwfdecAsContext
*cx
,
236 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
237 SwfdecAsValue
*retval
)
239 SwfdecTestHTTPRequest
*request
;
242 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "v", &val
);
244 if (request
->state
> SWFDEC_TEST_HTTP_REQUEST_STATE_WAITING
) {
245 swfdec_test_throw (cx
, "Headers have already been sent");
249 if (SWFDEC_AS_VALUE_IS_NULL (&val
) || SWFDEC_AS_VALUE_IS_UNDEFINED (&val
)) {
250 soup_message_headers_remove (request
->message
->response_headers
,
253 soup_message_headers_replace (request
->message
->response_headers
,
254 "Content-Type", swfdec_as_value_to_string (cx
, &val
));
258 SWFDEC_TEST_FUNCTION ("HTTPRequest_get_statusCode", swfdec_test_http_request_get_statusCode
, 0)
260 swfdec_test_http_request_get_statusCode (SwfdecAsContext
*cx
,
261 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
262 SwfdecAsValue
*retval
)
264 SwfdecTestHTTPRequest
*request
;
266 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "");
268 SWFDEC_AS_VALUE_SET_INT (retval
, request
->message
->status_code
);
271 SWFDEC_TEST_FUNCTION ("HTTPRequest_set_statusCode", swfdec_test_http_request_set_statusCode
, 0)
273 swfdec_test_http_request_set_statusCode (SwfdecAsContext
*cx
,
274 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
275 SwfdecAsValue
*retval
)
277 SwfdecTestHTTPRequest
*request
;
280 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "i", &status_code
);
285 if (request
->state
> SWFDEC_TEST_HTTP_REQUEST_STATE_WAITING
) {
286 swfdec_test_throw (cx
, "Headers have already been sent");
290 soup_message_set_status (request
->message
, status_code
);
293 SWFDEC_TEST_FUNCTION ("HTTPRequest_send", swfdec_test_http_request_send
, 0)
295 swfdec_test_http_request_send (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
296 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
298 SwfdecTestHTTPRequest
*request
;
301 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "v", &val
);
303 if (request
->state
> SWFDEC_TEST_HTTP_REQUEST_STATE_SENDING
) {
304 swfdec_test_throw (cx
, "Reply has already been sent");
308 if (SWFDEC_AS_VALUE_IS_OBJECT (&val
) &&
309 SWFDEC_IS_TEST_BUFFER (SWFDEC_AS_VALUE_GET_OBJECT (&val
))) {
310 SwfdecTestBuffer
*buffer
=
311 SWFDEC_TEST_BUFFER (SWFDEC_AS_VALUE_GET_OBJECT (&val
));
312 soup_message_body_append (request
->message
->response_body
,
313 SOUP_MEMORY_COPY
, buffer
->buffer
->data
, buffer
->buffer
->length
);
315 const char *data
= swfdec_as_value_to_string (cx
, &val
);
316 soup_message_body_append (request
->message
->response_body
,
317 SOUP_MEMORY_COPY
, data
, strlen (data
));
320 soup_server_unpause_message (request
->server
->server
, request
->message
);
322 swfdec_test_http_server_run (request
->server
);
324 request
->state
= SWFDEC_TEST_HTTP_REQUEST_STATE_SENDING
;
327 SWFDEC_TEST_FUNCTION ("HTTPRequest_close", swfdec_test_http_request_close
, 0)
329 swfdec_test_http_request_close (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
330 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*retval
)
332 SwfdecTestHTTPRequest
*request
;
334 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_HTTP_REQUEST
, &request
, "");
336 if (request
->state
== SWFDEC_TEST_HTTP_REQUEST_STATE_SENT
) {
337 swfdec_test_throw (cx
, "Reply has already been sent");
341 soup_message_body_complete (request
->message
->response_body
);
342 soup_server_unpause_message (request
->server
->server
, request
->message
);
344 swfdec_test_http_server_run (request
->server
);
346 request
->state
= SWFDEC_TEST_HTTP_REQUEST_STATE_SENT
;