2
* Copyright (C) 2012 Openismus GmbH
4
* Authors: Jens Georg <jensg@openismus.com>
5
* Mathias Hasselmann <mathias@openismus.com>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public License
9
* as published by the Free Software Foundation; version 2.1 of
10
* the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28
#include <glib/gstdio.h>
30
#include <libsoup/soup.h>
33
#define _GRILO_H_INSIDE_
36
#include "grl-net-mock-private.h"
37
#include "grl-net-private.h"
39
static GKeyFile *config = NULL;
40
static GRegex *ignored_parameters = NULL;
41
static char *base_path = NULL;
42
static gboolean enable_mocking = FALSE;
47
return enable_mocking;
51
get_url_mocked (GrlNetWc *self,
55
GCancellable *cancellable)
57
char *data_file, *full_path;
62
if (ignored_parameters) {
63
SoupURI *uri = soup_uri_new (url);
64
char *new_query = g_regex_replace (ignored_parameters,
65
soup_uri_get_query (uri), -1, 0,
67
soup_uri_set_query (uri, *new_query ? new_query : NULL);
68
new_url = soup_uri_to_string (uri, FALSE);
71
new_url = g_strdup (url);
75
g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (result),
77
GRL_NET_WC_ERROR_NETWORK_ERROR,
79
"No mock definition found");
80
g_simple_async_result_complete_in_idle (G_SIMPLE_ASYNC_RESULT (result));
84
data_file = g_key_file_get_value (config, new_url, "data", &error);
86
g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (result),
88
GRL_NET_WC_ERROR_NOT_FOUND,
89
"Could not find mock content: %s",
92
g_simple_async_result_complete_in_idle (G_SIMPLE_ASYNC_RESULT (result));
95
if (data_file[0] != '/') {
96
full_path = g_build_filename (base_path, data_file, NULL);
98
full_path = data_file;
102
if (g_stat (full_path, &stat_buf) < 0) {
103
g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (result),
105
GRL_NET_WC_ERROR_NOT_FOUND,
107
"Could not access mock content");
108
g_simple_async_result_complete_in_idle (G_SIMPLE_ASYNC_RESULT (result));
120
g_simple_async_result_set_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result),
123
g_simple_async_result_complete_in_idle (G_SIMPLE_ASYNC_RESULT (result));
127
get_content_mocked (GrlNetWc *self,
132
char *url = (char *) op;
133
char *data_file = NULL, *full_path = NULL;
134
GError *error = NULL;
136
data_file = g_key_file_get_value (config, url, "data", NULL);
137
if (data_file[0] != '/') {
138
full_path = g_build_filename (base_path, data_file, NULL);
140
full_path = data_file;
143
g_file_get_contents (full_path, content, length, &error);
152
void init_mock_requester (GrlNetWc *self)
154
char *config_filename = g_strdup (g_getenv (GRL_NET_MOCKED_VAR));
155
enable_mocking = FALSE;
157
if (config_filename == NULL) {
161
/* Read configuration file. */
162
GError *error = NULL;
163
config = g_key_file_new ();
165
GRL_DEBUG ("Loading mock responses from \"%s\"", config_filename);
166
g_key_file_load_from_file (config, config_filename, G_KEY_FILE_NONE, &error);
171
GRL_WARNING ("Failed to load mock file \"%s\": %s",
172
config_filename, error->message);
173
g_clear_error (&error);
175
/* Check if we managed to load a file */
176
version = g_key_file_get_integer (config, "default", "version", &error);
178
if (error || version < GRL_NET_MOCK_VERSION) {
179
GRL_WARNING ("Unsupported mock version.");
180
g_clear_error (&error);
182
enable_mocking = TRUE;
186
if (!enable_mocking) {
187
g_free (config_filename);
188
g_key_file_unref (config);
193
char **parameter_names = g_key_file_get_string_list (config, "default",
194
"ignored-parameters",
197
/* Build regular expressions for ignored query parameters. */
198
if (parameter_names) {
199
GString *pattern = g_string_new ("(?:^|\\&)");
201
if (parameter_names[0] && strcmp(parameter_names[0], "*") == 0) {
202
g_string_append (pattern, "[^=&]+");
204
g_string_append (pattern, "(?:");
206
for (int i = 0; parameter_names[i]; ++i) {
208
g_string_append (pattern, "|");
210
char *escaped = g_regex_escape_string (parameter_names[i], -1);
211
g_string_append (pattern, escaped);
215
g_string_append (pattern, ")(?:=[^&]*)?");
218
ignored_parameters = g_regex_new (pattern->str, G_REGEX_OPTIMIZE, 0, &error);
221
GRL_WARNING ("Failed to compile regular expression "
222
"for ignored query parameters: %s", error->message);
223
g_clear_error (&error);
227
/* Find base path for mock data. */
228
GFile *file = g_file_new_for_commandline_arg (config_filename);
229
GFile *parent = g_file_get_parent (file);
231
base_path = g_file_get_path (parent);
233
g_object_unref (parent);
234
g_object_unref (file);
235
g_free (config_filename);
238
void finalize_mock_requester (GrlNetWc *self)
241
g_key_file_unref (config);
248
if (ignored_parameters) {
249
g_regex_unref (ignored_parameters);
253
void free_mock_op_res (void *op)