Skip to content

Commit 217dbd4

Browse files
committed
update test suite
1 parent f2b6af7 commit 217dbd4

File tree

2 files changed

+265
-30
lines changed

2 files changed

+265
-30
lines changed

build_test_resources/cfuntest.c

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@ int is_service_on = 0;
1111
void ngx_http_c_func_init(ngx_http_c_func_ctx_t* ctx) {
1212
ngx_http_c_func_log(info, ctx, "%s", "Starting The Application");
1313

14-
char* my_cache_value = ngx_http_c_func_cache_new(ctx->shared_mem, "key", sizeof("This is cache value") + 1);
15-
16-
if (my_cache_value) {
17-
memset(my_cache_value, 0, sizeof("This is cache value") + 1 );
18-
strcpy(my_cache_value, "This is cache value");
19-
}
20-
2114
is_service_on = 1;
22-
2315
}
2416

2517

@@ -87,23 +79,49 @@ void my_app_simple_get_header_param(ngx_http_c_func_ctx_t *ctx) {
8779
}
8880
}
8981

90-
void my_simple_extra_foo_header_input(ngx_http_c_func_ctx_t *ctx) {
82+
char* login(const char* userId, const char*pass) {
83+
return "foo";
84+
}
9185

92-
ngx_http_c_func_add_header_in(ctx, "foo", sizeof("foo")-1, "foovalue", sizeof("foovalue")-1);
86+
void my_simple_authentication(ngx_http_c_func_ctx_t *ctx) {
9387

94-
ngx_http_c_func_write_resp(
95-
ctx,
96-
200,
97-
"200 OK",
98-
"text/plain",
99-
"Extra Header foo",
100-
sizeof("Extra Header foo") - 1
101-
);
88+
ngx_http_c_func_log_info(ctx, "Authenticating");
89+
char *userId = (char*) ngx_http_c_func_get_header(ctx, "userId");
90+
char *userPass = (char*) ngx_http_c_func_get_header(ctx, "userPass");
91+
char* userName;
92+
93+
if ( userId == NULL || strlen(userId) == 0) {
94+
AUTH_FAILED:
95+
ngx_http_c_func_write_resp(
96+
ctx,
97+
403,
98+
"403 Authenthication Failed",
99+
"text/plain",
100+
"",
101+
0
102+
);
103+
} else {
104+
userName = login(userId, userPass);
105+
/** Add input header for downstream response **/
106+
if (userName) {
107+
ngx_http_c_func_add_header_in(ctx, "userName", sizeof("userName")-1, userName, strlen(userName));
108+
} else {
109+
goto AUTH_FAILED;
110+
}
111+
112+
ngx_http_c_func_write_resp(
113+
ctx,
114+
200,
115+
"200 OK",
116+
"text/plain",
117+
"OK",
118+
sizeof("OK")-1
119+
);
120+
}
102121
}
103122

104123
void my_simple_extra_foo_header_output(ngx_http_c_func_ctx_t *ctx) {
105-
106-
ngx_http_c_func_add_header_out(ctx, "foo", sizeof("foo")-1, "foovalue", sizeof("foovalue")-1);
124+
ngx_http_c_func_add_header_out(ctx, "foo", sizeof("foo") - 1, "foovalue", sizeof("foovalue") - 1);
107125

108126
ngx_http_c_func_write_resp(
109127
ctx,
@@ -164,12 +182,35 @@ void my_app_simple_post(ngx_http_c_func_ctx_t *ctx) {
164182
202,
165183
"202 Accepted and Processing",
166184
"text/plain",
167-
ctx->req_body,
185+
ctx->req_body,
168186
ctx->req_body_len
169187
);
170188
}
171189
}
172190

191+
void my_app_simple_set_cache(ngx_http_c_func_ctx_t *ctx) {
192+
ngx_http_c_func_log_info(ctx, "logged from my_app_simple_set_cache");
193+
194+
char* my_cache_value = ngx_http_c_func_cache_new(ctx->shared_mem, "key", sizeof("This is cache value") + 1);
195+
196+
if (my_cache_value) {
197+
memset(my_cache_value, 0, sizeof("This is cache value") + 1 );
198+
strcpy(my_cache_value, "This is cache value");
199+
}
200+
201+
if (my_cache_value) {
202+
ngx_http_c_func_write_resp(
203+
ctx,
204+
200,
205+
"200 OK",
206+
"text/plain",
207+
"OK",
208+
sizeof("OK") - 1
209+
);
210+
}
211+
}
212+
213+
173214
void my_app_simple_get_cache(ngx_http_c_func_ctx_t *ctx) {
174215
ngx_http_c_func_log_info(ctx, "logged from my_app_simple_get_cache");
175216

@@ -190,7 +231,6 @@ void my_app_simple_get_cache(ngx_http_c_func_ctx_t *ctx) {
190231
void my_app_simple_get_no_resp(ngx_http_c_func_ctx_t *ctx) {
191232
ngx_http_c_func_log_info(ctx, "Calling back and log from my_app_simple_get_no_resp");
192233

193-
194234
}
195235

196236

build_test_resources/sanity_test_raw_parse.t

Lines changed: 203 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use lib 'inc';
44
use lib 'lib';
55
use Test::Nginx::Socket 'no_plan';
66

7+
our $main_conf = <<'_EOC_';
8+
thread_pool my_thread_pool threads=8 max_queue=8;
9+
_EOC_
10+
711
no_long_string();
812
913
run_tests();
@@ -77,6 +81,7 @@ qr/QVNKS0pDQVNLTEpDS0xBU0pXbGtlandrbGplIGpka2FqbGthc2tsZGtqbHNrICBrZGpha2xzZGphc
7781
--- config
7882
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
7983
location = /testCFUNCERRORRESP {
84+
error_log /dev/null;
8085
ngx_http_c_func_call "my_app_simple_get_no_resp";
8186
}
8287
--- request
@@ -119,22 +124,64 @@ qr/greeting=enjoy-http-c-function-testing$/
119124
120125
=== TEST 8: Set C_FUNC_TEST_CACHE
121126
--- config
122-
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
127+
ngx_http_c_func_link_lib "/home/taymindis/github/nginx-c-function/t/libcfuntest.so";
123128
location = /testCFunGetCache {
124129
ngx_http_c_func_call "my_app_simple_get_cache";
125130
}
131+
location = /testCFunSetCache {
132+
ngx_http_c_func_call "my_app_simple_set_cache";
133+
}
134+
--- pipelined_requests eval
135+
["POST /testCFunSetCache", "GET /testCFunGetCache"]
136+
--- response_body eval
137+
["OK", "This is cache value"]
138+
139+
140+
=== TEST 9: Test output headers
141+
--- config
142+
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
143+
location = /ext_header_foo {
144+
ngx_http_c_func_call "my_simple_extra_foo_header_output";
145+
}
126146
--- request
127-
POST /testCFunGetCache
147+
GET /ext_header_foo
128148
--- error_code: 200
129149
--- response_headers
130-
Content-Type: text/plain
150+
foo: foovalue
151+
152+
153+
154+
=== TEST 10: Authentication
155+
--- config
156+
ngx_http_c_func_link_lib "/home/taymindis/github/nginx-c-function/t/libcfuntest.so";
157+
location /backend {
158+
return 200 "Welcome ${arg_userName}";
159+
}
160+
location = /auth {
161+
internal;
162+
ngx_http_c_func_call "my_simple_authentication";
163+
}
164+
location = /my_simple_authentication {
165+
auth_request /auth;
166+
proxy_pass http://127.0.0.1:${server_port}/backend?userName=$http_userName;
167+
}
168+
--- request
169+
GET /my_simple_authentication
170+
--- more_headers
171+
userId:foo
172+
userPass:asdasds
173+
--- error_code: 200
131174
--- response_body_like eval
132-
qr/This is cache value$/
175+
qr/Welcome foo$/
176+
177+
178+
# Test Suite below is aio threads
133179
134180
135-
=== TEST 9: Set C_FUNC_AIO_THREADS_TEST
181+
=== TEST 11: Set C_FUNC_TEST_1
182+
--- main_config eval: $::main_conf
136183
--- config
137-
aio threads;
184+
aio threads=my_thread_pool;
138185
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
139186
location = /testCFunGreeting {
140187
ngx_http_c_func_call "my_app_simple_get_greeting";
@@ -148,9 +195,130 @@ Content-Type: text/plain
148195
qr/greeting from ngx_http_c_func testing$/
149196
150197
151-
=== TEST 10: Test output headers
198+
=== TEST 12: Set C_FUNC_TEST_ARGS
199+
--- main_config eval: $::main_conf
152200
--- config
153-
aio threads;
201+
aio threads=my_thread_pool;
202+
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
203+
location = /testCFunARGS {
204+
ngx_http_c_func_call "my_app_simple_get_args";
205+
}
206+
--- request
207+
GET /testCFunARGS?greeting=hello_nginx?id=129310923
208+
--- error_code: 200
209+
--- response_headers
210+
Content-Type: text/plain
211+
--- response_body_like eval
212+
qr/greeting=hello_nginx\?id=129310923$/
213+
214+
215+
=== TEST 13: Set C_FUNC_TEST_POST_NONE
216+
--- main_config eval: $::main_conf
217+
--- config
218+
aio threads=my_thread_pool;
219+
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
220+
location = /testCFunPOSTBody {
221+
ngx_http_c_func_call "my_app_simple_post";
222+
}
223+
--- request
224+
POST /testCFunPOSTBody
225+
" "
226+
--- error_code: 202
227+
--- response_headers
228+
Content-Type: text/plain
229+
--- response_body_like eval
230+
qr/\s/
231+
232+
233+
=== TEST 14: Set C_FUNC_TEST_GET_TOKEN
234+
--- main_config eval: $::main_conf
235+
--- config
236+
aio threads=my_thread_pool;
237+
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
238+
location = /testCFunCVerifyToken {
239+
ngx_http_c_func_call "my_app_simple_get_token_args";
240+
}
241+
--- request
242+
GET /testCFunCVerifyToken?token=QVNKS0pDQVNLTEpDS0xBU0pXbGtlandrbGplIGpka2FqbGthc2tsZGtqbHNrICBrZGpha2xzZGphc2Rhcw==
243+
--- error_code: 401
244+
--- response_headers
245+
Content-Type: text/plain
246+
--- response_body_like eval
247+
qr/QVNKS0pDQVNLTEpDS0xBU0pXbGtlandrbGplIGpka2FqbGthc2tsZGtqbHNrICBrZGpha2xzZGphc2Rhcw==$/
248+
249+
250+
=== TEST 15: Set C_FUNC_TEST_GET_ERROR_RESP
251+
--- main_config eval: $::main_conf
252+
--- config
253+
aio threads=my_thread_pool;
254+
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
255+
location = /testCFUNCERRORRESP {
256+
error_log /dev/null;
257+
ngx_http_c_func_call "my_app_simple_get_no_resp";
258+
}
259+
--- request
260+
GET /testCFUNCERRORRESP?token=QVNKS0pDQVNLTEpDS0xBU0pXbGtlandrbGplIGpka2FqbGthc2tsZGtqbHNrICBrZGpha2xzZGphc2Rhcw==
261+
--- error_code: 500
262+
--- response_headers
263+
Content-Type: text/html
264+
265+
266+
=== TEST 16: Set C_FUNC_TEST_GET_CALLOC_FROM_POOL
267+
--- main_config eval: $::main_conf
268+
--- config
269+
aio threads=my_thread_pool;
270+
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
271+
location = /testCFUNCCallocFromPool {
272+
ngx_http_c_func_call "my_app_simple_get_calloc_from_pool";
273+
}
274+
--- request
275+
GET /testCFUNCCallocFromPool
276+
--- error_code: 200
277+
--- response_headers
278+
Content-Type: text/plain
279+
--- response_body_like eval
280+
qr/This is the message calloc from pool$/
281+
282+
283+
=== TEST 17: Set C_FUNC_TEST_POST_BODY
284+
--- main_config eval: $::main_conf
285+
--- config
286+
aio threads=my_thread_pool;
287+
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
288+
location = /testCFunPOSTBody {
289+
ngx_http_c_func_call "my_app_simple_post";
290+
}
291+
--- request
292+
POST /testCFunPOSTBody
293+
greeting=enjoy-http-c-function-testing
294+
--- error_code: 202
295+
--- response_headers
296+
Content-Type: text/plain
297+
--- response_body_like eval
298+
qr/greeting=enjoy-http-c-function-testing$/
299+
300+
301+
=== TEST 18: Set C_FUNC_TEST_CACHE
302+
--- main_config eval: $::main_conf
303+
--- config
304+
aio threads=my_thread_pool;
305+
ngx_http_c_func_link_lib "/home/taymindis/github/nginx-c-function/t/libcfuntest.so";
306+
location = /testCFunGetCache {
307+
ngx_http_c_func_call "my_app_simple_get_cache";
308+
}
309+
location = /testCFunSetCache {
310+
ngx_http_c_func_call "my_app_simple_set_cache";
311+
}
312+
--- pipelined_requests eval
313+
["POST /testCFunSetCache", "GET /testCFunGetCache"]
314+
--- response_body eval
315+
["OK", "This is cache value"]
316+
317+
318+
=== TEST 19: output headers
319+
--- main_config eval: $::main_conf
320+
--- config
321+
aio threads=my_thread_pool;
154322
ngx_http_c_func_link_lib "NGINX_HTTP_C_FUNCTION_TEST_LIB_PATH/libcfuntest.so";
155323
location = /ext_header_foo {
156324
ngx_http_c_func_call "my_simple_extra_foo_header_output";
@@ -161,3 +329,30 @@ GET /ext_header_foo
161329
--- response_headers
162330
foo: foovalue
163331
332+
333+
334+
=== TEST 20: Authentication
335+
--- main_config eval: $::main_conf
336+
--- config
337+
aio threads=my_thread_pool;
338+
ngx_http_c_func_link_lib "/home/taymindis/github/nginx-c-function/t/libcfuntest.so";
339+
location /backend {
340+
return 200 "Welcome ${arg_userName}";
341+
}
342+
location = /auth {
343+
internal;
344+
ngx_http_c_func_call "my_simple_authentication";
345+
}
346+
location = /my_simple_authentication {
347+
auth_request /auth;
348+
proxy_pass http://127.0.0.1:${server_port}/backend?userName=$http_userName;
349+
}
350+
--- request
351+
GET /my_simple_authentication
352+
--- more_headers
353+
userId:foo
354+
userPass:asdasds
355+
--- error_code: 200
356+
--- response_body_like eval
357+
qr/Welcome foo$/
358+

0 commit comments

Comments
 (0)