1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-06 05:02:40 +03:00

Added a unit test for websocket_client functionality, other small fixes

This commit is contained in:
Jordan
2014-08-21 14:53:47 -04:00
parent 5e7362ea5a
commit a4eca7c492
5 changed files with 74 additions and 3 deletions

View File

@@ -17,7 +17,7 @@
#define PORT "8888" #define PORT "8888"
#define SSL_CERT "./ssl/server.pem" #define SSL_CERT "./ssl/server.pem"
int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len) static int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len)
{ {
printf("From server: %s\r\n", data); printf("From server: %s\r\n", data);

View File

@@ -573,7 +573,20 @@ CIVETWEB_API void mg_cry(struct mg_connection *conn,
/* utility method to compare two buffers, case incensitive. */ /* utility method to compare two buffers, case incensitive. */
CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len); CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len);
/* Connect to a websocket as a client */ /* Connect to a websocket as a client
Parameters:
host: host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or "localhost"
port: server port
use_ssl: make a secure connection to server
error_buffer, error_buffer_size: error message placeholder.
path: server path you are trying to connect to, i.e. if connection to localhost/app, path should be "/app"
origin: value of the Origin HTTP header
data_func: callback that should be used when data is received from the server
Return:
On success, valid mg_connection object.
On error, NULL. */
typedef int (*websocket_data_func)(struct mg_connection *, int bits, typedef int (*websocket_data_func)(struct mg_connection *, int bits,
char *data, size_t data_len); char *data, size_t data_len);
CIVETWEB_API struct mg_connection *mg_client_websocket_connect(const char *host, int port, int use_ssl, CIVETWEB_API struct mg_connection *mg_client_websocket_connect(const char *host, int port, int use_ssl,

View File

@@ -6615,9 +6615,10 @@ struct mg_connection *mg_client_websocket_connect(const char *host, int port, in
"\r\n", path, host, magic, origin); "\r\n", path, host, magic, origin);
//Connection object will be null if something goes wrong //Connection object will be null if something goes wrong
if(conn == NULL) if(conn == NULL || (strcmp(conn->request_info.uri, "101") != 0))
{ {
DEBUG_TRACE("Websocket client connect error: %s\r\n", error_buffer); DEBUG_TRACE("Websocket client connect error: %s\r\n", error_buffer);
if(conn != NULL) { mg_free(conn); conn = NULL; }
return conn; return conn;
} }

View File

@@ -525,6 +525,57 @@ static void test_mg_download(int use_ssl) {
mg_stop(ctx); mg_stop(ctx);
} }
static int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len)
{
return 1;
}
static void test_mg_client_websocket_connect(int use_ssl) {
struct mg_connection* conn;
char ebuf[100];
int port = HTTP_PORT;
if(use_ssl) { port = HTTPS_PORT; }
//Try to connect to our own server
//TODO: These do not work right now
//Invalid port test
/*conn = mg_client_websocket_connect("localhost", 0, use_ssl,
ebuf, sizeof(ebuf),
"/", "http://localhost",websocket_data_handler);
ASSERT(conn == NULL);
//Should succeed, the default civetweb sever should complete the handshake
conn = mg_client_websocket_connect("localhost", port, use_ssl,
ebuf, sizeof(ebuf),
"/", "http://localhost",websocket_data_handler);
ASSERT(conn != NULL);*/
//Try an external server test
port = 80;
if(use_ssl) { port = 443; }
//Not a websocket server path
conn = mg_client_websocket_connect("websocket.org", port, use_ssl,
ebuf, sizeof(ebuf),
"/", "http://websocket.org",websocket_data_handler);
ASSERT(conn == NULL);
//Invalid port test
conn = mg_client_websocket_connect("echo.websocket.org", 0, use_ssl,
ebuf, sizeof(ebuf),
"/", "http://websocket.org",websocket_data_handler);
ASSERT(conn == NULL);
//Should succeed, echo.websocket.org echos the data back
conn = mg_client_websocket_connect("echo.websocket.org", port, use_ssl,
ebuf, sizeof(ebuf),
"/", "http://websocket.org",websocket_data_handler);
ASSERT(conn != NULL);
}
static int alloc_printf(char **buf, size_t size, char *fmt, ...) { static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
@@ -1016,6 +1067,12 @@ int __cdecl main(void) {
#ifndef NO_SSL #ifndef NO_SSL
test_mg_download(1); test_mg_download(1);
#endif #endif
test_mg_client_websocket_connect(0);
#ifndef NO_SSL
test_mg_client_websocket_connect(1);
#endif
test_mg_upload(); test_mg_upload();
test_request_replies(); test_request_replies();
test_api_calls(); test_api_calls();