1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#include "http.h"
#include <sqlite3.h>
#include "test.h"
SQLITE_EXTENSION_INIT3
void http_backend_dummy_set_errmsg(const char* zErrMsg);
void http_backend_dummy_set_response(http_response* response);
void http_backend_dummy_reset_request();
const http_request* http_backend_dummy_get_last_request();
int sqlite3_http_init(sqlite3*, char**, const sqlite3_api_routines*);
void new_text_response(http_response* response,
const char* zBody,
const char* zHeaders,
int iStatusCode,
const char* zStatus) {
memset(response, 0, sizeof(*response));
assert(zStatus != NULL);
if (zBody) {
response->pBody = sqlite3_mprintf("%s", zBody);
response->szBody = strlen(zBody);
}
if (zHeaders) {
response->zHeaders = sqlite3_mprintf("%s", zHeaders);
response->szHeaders = strlen(zHeaders);
}
response->iStatusCode = iStatusCode;
response->zStatus = sqlite3_mprintf("%s", zStatus);
}
static sqlite3* db;
void test_http_get() {
sqlite3_stmt* stmt;
http_response response;
new_text_response(&response, "hello, world!", "Foo: Bar\r\n\r\n", 200, "HTTP/1.0 200 OK");
http_backend_dummy_set_response(&response);
ASSERT_INT_EQ(
sqlite3_prepare_v2(db, "select * from http_get('http://example.com')", -1, &stmt, NULL),
SQLITE_OK);
ASSERT_INT_EQ(sqlite3_step(stmt), SQLITE_ROW);
ASSERT_INT_EQ(sqlite3_column_count(stmt), 4);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 0), "HTTP/1.0 200 OK");
ASSERT_INT_EQ(sqlite3_column_int(stmt, 1), 200);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 2), "Foo: Bar\r\n\r\n");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 3), "hello, world!");
ASSERT_INT_EQ(sqlite3_finalize(stmt), SQLITE_OK);
}
void test_http_get_hidden_columns() {
sqlite3_stmt* stmt;
http_response response;
new_text_response(&response, "hello, world!", "Foo: Bar\r\n\r\n", 200, "HTTP/1.0 200 OK");
http_backend_dummy_set_response(&response);
ASSERT_INT_EQ(
sqlite3_prepare_v2(db,
"select response_status, response_status_code, response_headers, "
"response_body, request_method, request_url, request_headers, "
"request_body from http_get('http://example.com/foo')",
-1,
&stmt,
NULL),
SQLITE_OK);
ASSERT_INT_EQ(sqlite3_step(stmt), SQLITE_ROW);
ASSERT_INT_EQ(sqlite3_column_count(stmt), 8);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 0), "HTTP/1.0 200 OK");
ASSERT_INT_EQ(sqlite3_column_int(stmt, 1), 200);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 2), "Foo: Bar\r\n\r\n");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 3), "hello, world!");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 4), "GET");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 5), "http://example.com/foo");
ASSERT_NULL(sqlite3_column_text(stmt, 6));
ASSERT_NULL(sqlite3_column_text(stmt, 7));
ASSERT_INT_EQ(sqlite3_finalize(stmt), SQLITE_OK);
}
void test_http_get_request_headers() {
sqlite3_stmt* stmt;
http_response response;
new_text_response(&response, "hello, world!", "Foo: Bar\r\n\r\n", 200, "HTTP/1.0 200 OK");
http_backend_dummy_set_response(&response);
ASSERT_INT_EQ(
sqlite3_prepare_v2(db,
"select response_status, response_status_code, response_headers, "
"response_body, request_method, request_url, request_headers, "
"request_body from http_get('http://example.com/foo', "
"http_headers('Req1', 'Val1', 'Req2', 'Val2'))",
-1,
&stmt,
NULL),
SQLITE_OK);
ASSERT_INT_EQ(sqlite3_step(stmt), SQLITE_ROW);
ASSERT_INT_EQ(sqlite3_column_count(stmt), 8);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 0), "HTTP/1.0 200 OK");
ASSERT_INT_EQ(sqlite3_column_int(stmt, 1), 200);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 2), "Foo: Bar\r\n\r\n");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 3), "hello, world!");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 4), "GET");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 5), "http://example.com/foo");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 6), "Req1: Val1\r\nReq2: Val2\r\n");
ASSERT_NULL(sqlite3_column_text(stmt, 7));
ASSERT_INT_EQ(sqlite3_finalize(stmt), SQLITE_OK);
}
void test_http_post() {
sqlite3_stmt* stmt;
http_response response;
new_text_response(&response, "hello, world!", "Foo: Bar\r\n\r\n", 200, "HTTP/1.0 200 OK");
http_backend_dummy_set_response(&response);
ASSERT_INT_EQ(
sqlite3_prepare_v2(db, "select * from http_post('http://example.com')", -1, &stmt, NULL),
SQLITE_OK);
ASSERT_INT_EQ(sqlite3_step(stmt), SQLITE_ROW);
ASSERT_INT_EQ(sqlite3_column_count(stmt), 4);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 0), "HTTP/1.0 200 OK");
ASSERT_INT_EQ(sqlite3_column_int(stmt, 1), 200);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 2), "Foo: Bar\r\n\r\n");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 3), "hello, world!");
ASSERT_INT_EQ(sqlite3_finalize(stmt), SQLITE_OK);
}
void test_http_post_hidden_columns() {
sqlite3_stmt* stmt;
http_response response;
new_text_response(&response, "hello, world!", "Foo: Bar\r\n\r\n", 200, "HTTP/1.0 200 OK");
http_backend_dummy_set_response(&response);
ASSERT_INT_EQ(
sqlite3_prepare_v2(db,
"select response_status, response_status_code, response_headers, "
"response_body, request_method, request_url, request_headers, "
"request_body from http_post('http://example.com/foo')",
-1,
&stmt,
NULL),
SQLITE_OK);
ASSERT_INT_EQ(sqlite3_step(stmt), SQLITE_ROW);
ASSERT_INT_EQ(sqlite3_column_count(stmt), 8);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 0), "HTTP/1.0 200 OK");
ASSERT_INT_EQ(sqlite3_column_int(stmt, 1), 200);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 2), "Foo: Bar\r\n\r\n");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 3), "hello, world!");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 4), "POST");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 5), "http://example.com/foo");
ASSERT_NULL(sqlite3_column_text(stmt, 6));
ASSERT_NULL(sqlite3_column_text(stmt, 7));
ASSERT_INT_EQ(sqlite3_finalize(stmt), SQLITE_OK);
}
void test_http_post_request_headers() {
sqlite3_stmt* stmt;
http_response response;
new_text_response(&response, "hello, world!", "Foo: Bar\r\n\r\n", 200, "HTTP/1.0 200 OK");
http_backend_dummy_set_response(&response);
ASSERT_INT_EQ(
sqlite3_prepare_v2(db,
"select response_status, response_status_code, response_headers, "
"response_body, request_method, request_url, request_headers, "
"request_body from http_post('http://example.com/foo', "
"http_headers('Req1', 'Val1', 'Req2', 'Val2'))",
-1,
&stmt,
NULL),
SQLITE_OK);
ASSERT_INT_EQ(sqlite3_step(stmt), SQLITE_ROW);
ASSERT_INT_EQ(sqlite3_column_count(stmt), 8);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 0), "HTTP/1.0 200 OK");
ASSERT_INT_EQ(sqlite3_column_int(stmt, 1), 200);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 2), "Foo: Bar\r\n\r\n");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 3), "hello, world!");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 4), "POST");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 5), "http://example.com/foo");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 6), "Req1: Val1\r\nReq2: Val2\r\n");
ASSERT_NULL(sqlite3_column_text(stmt, 7));
ASSERT_INT_EQ(sqlite3_finalize(stmt), SQLITE_OK);
}
void test_http_post_request_body() {
sqlite3_stmt* stmt;
http_response response;
new_text_response(&response, "hello, world!", "Foo: Bar\r\n\r\n", 200, "HTTP/1.0 200 OK");
http_backend_dummy_set_response(&response);
ASSERT_INT_EQ(
sqlite3_prepare_v2(db,
"select response_status, response_status_code, response_headers, "
"response_body, request_method, request_url, request_headers, "
"request_body from http_post('http://example.com/foo', "
"http_headers('Req1', 'Val1', 'Req2', 'Val2'), 'hello')",
-1,
&stmt,
NULL),
SQLITE_OK);
ASSERT_INT_EQ(sqlite3_step(stmt), SQLITE_ROW);
ASSERT_INT_EQ(sqlite3_column_count(stmt), 8);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 0), "HTTP/1.0 200 OK");
ASSERT_INT_EQ(sqlite3_column_int(stmt, 1), 200);
ASSERT_STR_EQ(sqlite3_column_text(stmt, 2), "Foo: Bar\r\n\r\n");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 3), "hello, world!");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 4), "POST");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 5), "http://example.com/foo");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 6), "Req1: Val1\r\nReq2: Val2\r\n");
ASSERT_STR_EQ(sqlite3_column_text(stmt, 7), "hello");
ASSERT_INT_EQ(sqlite3_finalize(stmt), SQLITE_OK);
}
int main(int argc, char const* argv[]) {
sqlite3_initialize();
sqlite3_auto_extension((void (*)(void))sqlite3_http_init);
ASSERT_INT_EQ(sqlite3_open(":memory:", &db), SQLITE_OK);
test_http_get();
test_http_get_hidden_columns();
test_http_get_request_headers();
test_http_post();
test_http_post_hidden_columns();
test_http_post_request_headers();
test_http_post_request_body();
return 0;
}
|