Beispiel #1
0
void t01_handle_static_request(){
	INIT_LOCAL();

	char ok;
	onion *server=onion_new(0);
	onion_listen_point *lp=onion_buffer_listen_point_new();
	onion_add_listen_point(server, NULL, NULL, lp);
	
	onion_request *request=onion_request_new(lp);
	FILL(request,"GET / HTTP/1.1\n");
	
	onion_handler *handler=onion_handler_static("Not ready",302);
	FAIL_IF_NOT(handler);
	onion_set_root_handler(server, handler);
	
	onion_response *response=onion_response_new(request);
	ok=onion_handler_handle(handler, request, response);
	FAIL_IF_NOT_EQUAL(ok, OCS_PROCESSED);
	onion_response_free(response);

	const char *buffer=onion_buffer_listen_point_get_buffer_data(request);
	FAIL_IF_EQUAL_STR(buffer,"");
	FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 302 REDIRECT\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "\r\nContent-Length: 9\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "libonion");
	FAIL_IF_STRSTR(buffer, "License: AGPL"); // License is now LGPL, no need to advertise
	FAIL_IF_STRSTR(buffer, "License");
	FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nNot ready");
	
	onion_request_free(request);
	onion_free(server);
	
	END_LOCAL();
}
Beispiel #2
0
void t02_several_add_methods(){
	INIT_TEST();
	
	onion_block *block=onion_block_new();
	
	FAIL_IF_EQUAL(block, NULL);
	
	int i;
	for (i=0;i<1024;i++){
		onion_block_add_char(block, (char)i);
	}
	onion_block_clear(block);
	
	onion_block_add_str(block, "first ");
	for (i=0;i<1024;i++)
		onion_block_add_str(block, "test ");
	
	FAIL_IF_NOT_STRSTR(onion_block_data(block), "test");
	
	for (i=0;i<1024;i++)
		onion_block_add_data(block, "world", 4);
	
	FAIL_IF_STRSTR(onion_block_data(block), "world");
	FAIL_IF_NOT_STRSTR(onion_block_data(block), "worl");	
	
	int s=onion_block_size(block);
	
	onion_block_add_block(block, block);
	FAIL_IF_NOT_EQUAL(onion_block_size(block), s+s);
	
	onion_block_free(block);
	
	END_TEST();
}
Beispiel #3
0
void t18_json_escape_codes(){
	INIT_LOCAL();

	onion_dict *d=onion_dict_from_json("{ \"hello\": \"Hello\\nworld\", \"second\":\"second\" }");
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "hello"), "Hello\nworld");
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "second"), "second");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\\uD83D\\uDE02\" }");
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "hello"), "😂");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\\uD83D\\uDE03\" }"); // Another code point
	FAIL_IF_STRSTR(onion_dict_get(d, "hello"), "😂");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\\u007b\" }"); // simple unicode
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "hello"), "{");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\\\"Quote\" }"); // Escape quote
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "hello"), "\"Quote");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\"Quote\" }"); // Must fail
	FAIL_IF_NOT_EQUAL(d, NULL);

	d=onion_dict_new();
	onion_dict_add(d, "hello", "Hello\nWorld\\", 0);
	onion_dict_add(d, "second", "123", 0);
	onion_block *b=onion_dict_to_json(d);
	FAIL_IF_NOT_EQUAL_STR(onion_block_data(b),"{\"hello\":\"Hello\\nWorld\\\\\", \"second\":\"123\"}");
	onion_block_free(b);
	onion_dict_free(d);

	d=onion_dict_new();
	onion_dict_add(d, "hello", "😂\t\n😂", 0);
	b=onion_dict_to_json(d);
	FAIL_IF_NOT_EQUAL_STR(onion_block_data(b),"{\"hello\":\"😂\\t\\n😂\"}");
	onion_block_free(b);
	onion_dict_free(d);

	d=onion_dict_new();
	onion_dict_add(d, "hello", "\02\03\x7f", 0);
	b=onion_dict_to_json(d);
	FAIL_IF_NOT_EQUAL_STR(onion_block_data(b),"{\"hello\":\"\\u0002\\u0003\\u007F\"}");
	onion_block_free(b);
	onion_dict_free(d);

	END_LOCAL();
}
Beispiel #4
0
void t03_full_cycle_http11(){
	INIT_LOCAL();
	
	onion *server=onion_new(0);
	onion_add_listen_point(server, NULL,NULL,onion_buffer_listen_point_new());
	onion_request *request;
	char buffer[4096];
	memset(buffer,0,sizeof(buffer));
	
	request=onion_request_new(server->listen_points[0]);
	FILL(request,"GET / HTTP/1.1\n");
	
	onion_response *response=onion_response_new(request);
	
	onion_response_set_length(response, 30);
	FAIL_IF_NOT_EQUAL(response->length,30);
	onion_response_write_headers(response);
	
	onion_response_write0(response,"123456789012345678901234567890");
	onion_response_flush(response);
	
	FAIL_IF_NOT_EQUAL(response->sent_bytes,30);
	
	onion_response_free(response);
	buffer[sizeof(buffer)-1]=0;
	strncpy(buffer,onion_buffer_listen_point_get_buffer_data(request),sizeof(buffer)-1);
	onion_request_free(request);
	onion_free(server);
	
	FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 200 OK\r\n");
	FAIL_IF_STRSTR(buffer, "Connection: Keep-Alive\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "Content-Length: 30\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "Server: libonion");
	FAIL_IF_NOT_STRSTR(buffer, "coralbits");
	FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\n123456789012345678901234567890");

	//FAIL_IF_NOT_EQUAL_STR(buffer, "HTTP/1.1 200 OK\r\nContent-Length: 30\r\nServer: libonion v0.1 - coralbits.com\r\n\r\n123456789012345678901234567890");
	
	END_LOCAL();
}