Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
570c8213
Commit
570c8213
authored
Mar 03, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaned unit test
parent
f521999e
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
133 deletions
+33
-133
unit_test.c
examples/unit_test.c
+33
-133
No files found.
examples/unit_test.c
View file @
570c8213
...
...
@@ -400,91 +400,6 @@ static const char *test_server(void) {
return
NULL
;
}
// This http client sends two requests using one connection
static
int
cb2
(
struct
mg_connection
*
conn
)
{
const
char
*
file1
=
"mongoose.h"
,
*
file2
=
"mongoose.c"
,
*
file_name
=
file2
;
char
*
buf
=
(
char
*
)
conn
->
connection_param
,
*
file_data
;
int
file_len
,
ret_val
=
1
;
switch
(
conn
->
status_code
)
{
case
MG_CONNECT_SUCCESS
:
mg_printf
(
conn
,
"GET /%s HTTP/1.1
\r\n\r\n
"
,
file1
);
strcat
(
buf
,
"a"
);
ret_val
=
0
;
break
;
case
MG_CONNECT_FAILURE
:
strcat
(
buf
,
"b"
);
break
;
case
MG_DOWNLOAD_FAILURE
:
strcat
(
buf
,
"c"
);
break
;
case
MG_DOWNLOAD_SUCCESS
:
if
(
!
strcmp
(
buf
,
"a"
))
{
// Send second request
mg_printf
(
conn
,
"GET /%s HTTP/1.1
\r\n\r\n
"
,
file2
);
file_name
=
file1
;
}
if
((
file_data
=
read_file
(
file_name
,
&
file_len
))
!=
NULL
)
{
if
((
size_t
)
file_len
==
conn
->
content_len
&&
memcmp
(
file_data
,
conn
->
content
,
file_len
)
==
0
)
{
strcat
(
buf
,
"d"
);
}
else
{
strcat
(
buf
,
"f"
);
}
free
(
file_data
);
}
ret_val
=
!
strcmp
(
buf
,
"ad"
)
?
0
:
1
;
break
;
default:
strcat
(
buf
,
"e"
);
break
;
}
return
ret_val
;
}
static
int
cb4h
(
struct
mg_connection
*
conn
)
{
int
result
=
MG_REQUEST_NOT_PROCESSED
;
if
(
!
strcmp
(
conn
->
uri
,
"/x"
))
{
mg_send_data
(
conn
,
":-)"
,
3
);
result
=
MG_REQUEST_PROCESSED
;
}
return
result
;
}
static
int
cb4
(
struct
mg_connection
*
conn
)
{
if
(
conn
->
status_code
==
MG_CONNECT_SUCCESS
)
{
mg_printf
(
conn
,
"%s"
,
"POST /x HTTP/1.0
\r\n
Content-Length: 999999
\r\n\r\n
"
);
return
MG_CLIENT_CONTINUE
;
}
else
{
sprintf
((
char
*
)
conn
->
connection_param
,
"%.*s"
,
(
int
)
conn
->
content_len
,
conn
->
content
);
}
return
MG_CLIENT_CLOSE
;
}
static
int
cb3
(
struct
mg_connection
*
conn
)
{
sprintf
((
char
*
)
conn
->
connection_param
,
"%d"
,
conn
->
status_code
);
return
MG_CLIENT_CLOSE
;
}
static
const
char
*
test_mg_connect
(
void
)
{
char
buf2
[
40
]
=
""
,
buf3
[
40
]
=
""
,
buf4
[
40
]
=
""
;
struct
mg_server
*
server
=
mg_create_server
(
NULL
,
NULL
);
// cb4h
struct
mg_connection
*
conn
;
ASSERT
(
mg_set_option
(
server
,
"listening_port"
,
LISTENING_ADDR
)
==
NULL
);
ASSERT
(
mg_set_option
(
server
,
"document_root"
,
"."
)
==
NULL
);
ASSERT
(
mg_connect
(
server
,
""
,
0
,
0
)
==
NULL
);
ASSERT
((
conn
=
mg_connect
(
server
,
"127.0.0.1"
,
atoi
(
HTTP_PORT
),
0
))
!=
NULL
);
ASSERT
((
conn
=
mg_connect
(
server
,
"127.0.0.1"
,
29
,
0
))
!=
NULL
);
ASSERT
((
conn
=
mg_connect
(
server
,
"127.0.0.1"
,
atoi
(
HTTP_PORT
),
0
))
!=
NULL
);
{
int
i
;
for
(
i
=
0
;
i
<
50
;
i
++
)
mg_poll_server
(
server
,
1
);
}
printf
(
"buf2: [%s]
\n
"
,
buf2
);
//ASSERT(strcmp(buf2, "add") == 0);
ASSERT
(
strcmp
(
buf3
,
"1"
)
==
0
);
ASSERT
(
strcmp
(
buf4
,
"500 Server Error
\n
POST size > 999"
)
==
0
);
mg_destroy_server
(
&
server
);
return
NULL
;
}
#define DISP "Content-Disposition: form/data; "
#define CRLF "\r\n"
#define BOUNDARY "--xyz"
...
...
@@ -520,46 +435,33 @@ static const char *test_parse_multipart(void) {
return
NULL
;
}
static
int
us1
(
struct
mg_connection
*
conn
)
{
static
const
char
*
file_name
=
"mongoose.h"
;
char
*
cp
=
(
char
*
)
conn
->
connection_param
;
int
file_size
;
static
int
evh2
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
char
*
file_data
,
*
cp
=
(
char
*
)
conn
->
connection_param
;
int
file_size
,
result
=
MG_FALSE
;
if
(
conn
->
status_code
==
MG_CONNECT_SUCCESS
)
{
switch
(
ev
)
{
case
MG_AUTH
:
result
=
MG_TRUE
;
break
;
case
MG_CONNECT
:
mg_printf
(
conn
,
"GET /%s HTTP/1.0
\r\n\r\n
"
,
cp
);
return
0
;
}
else
if
(
conn
->
status_code
==
MG_DOWNLOAD_SUCCESS
)
{
char
*
file_data
=
read_file
(
file_name
,
&
file_size
);
result
=
MG_TRUE
;
break
;
case
MG_REQUEST
:
break
;
case
MG_REPLY
:
file_data
=
read_file
(
"unit_test.c"
,
&
file_size
);
sprintf
(
cp
,
"%d %s"
,
(
size_t
)
file_size
==
conn
->
content_len
&&
memcmp
(
file_data
,
conn
->
content
,
file_size
)
==
0
?
1
:
0
,
conn
->
query_string
==
NULL
?
"?"
:
conn
->
query_string
);
free
(
file_data
);
}
else
if
(
conn
->
status_code
==
MG_CONNECT_FAILURE
)
{
sprintf
(
cp
,
"%s"
,
"cf"
);
}
else
{
sprintf
(
cp
,
"%s"
,
"df"
);
break
;
default:
break
;
}
return
1
;
}
#ifdef MONGOOSE_USE_SSL
static
const
char
*
test_ssl
(
void
)
{
static
const
char
*
ssl_cert
=
"examples/ssl_cert.pem"
;
char
buf1
[
100
]
=
"mongoose.h"
;
struct
mg_server
*
server
=
mg_create_server
(
NULL
);
ASSERT
(
mg_set_option
(
server
,
"listening_port"
,
LISTENING_ADDR
)
==
NULL
);
ASSERT
(
mg_set_option
(
server
,
"document_root"
,
"."
)
==
NULL
);
ASSERT
(
mg_set_option
(
server
,
"ssl_certificate"
,
ssl_cert
)
==
NULL
);
ASSERT
(
mg_connect
(
server
,
"127.0.0.1"
,
atoi
(
HTTP_PORT
),
1
,
us1
,
buf1
)
==
1
);
{
int
i
;
for
(
i
=
0
;
i
<
50
;
i
++
)
mg_poll_server
(
server
,
1
);
}
ASSERT
(
strcmp
(
buf1
,
"1 ?"
)
==
0
);
mg_destroy_server
(
&
server
);
return
NULL
;
return
result
;
}
#endif
static
const
char
*
test_mg_set_option
(
void
)
{
struct
mg_server
*
server
=
mg_create_server
(
NULL
,
NULL
);
...
...
@@ -571,17 +473,19 @@ static const char *test_mg_set_option(void) {
static
const
char
*
test_rewrites
(
void
)
{
char
buf1
[
100
]
=
"xx"
;
struct
mg_server
*
server
=
mg_create_server
(
NULL
,
NULL
);
struct
mg_server
*
server
=
mg_create_server
(
NULL
,
evh2
);
struct
mg_connection
*
conn
;
const
char
*
port
;
ASSERT
(
mg_set_option
(
server
,
"listening_port"
,
"0"
)
==
NULL
);
ASSERT
(
mg_set_option
(
server
,
"document_root"
,
"."
)
==
NULL
);
ASSERT
(
mg_set_option
(
server
,
"url_rewrites"
,
"/xx=
./mongoose.h
"
)
==
NULL
);
ASSERT
(
mg_connect
(
server
,
"127.0.0.1"
,
atoi
(
mg_get_option
(
server
,
"listening_port"
)),
0
)
!=
NULL
)
;
ASSERT
(
mg_set_option
(
server
,
"url_rewrites"
,
"/xx=
unit_test.c
"
)
==
NULL
);
ASSERT
((
port
=
mg_get_option
(
server
,
"listening_port"
))
!=
NULL
);
ASSERT
(
(
conn
=
mg_connect
(
server
,
"127.0.0.1"
,
atoi
(
port
),
0
))
!=
NULL
);
conn
->
connection_param
=
buf1
;
{
int
i
;
for
(
i
=
0
;
i
<
50
;
i
++
)
mg_poll_server
(
server
,
1
);
}
//printf("[%s]\n", buf1);
ASSERT
(
strcmp
(
buf1
,
"1 ?"
)
==
0
);
mg_destroy_server
(
&
server
);
return
NULL
;
...
...
@@ -601,11 +505,7 @@ static const char *run_all_tests(void) {
RUN_TEST
(
test_parse_multipart
);
RUN_TEST
(
test_mg_set_option
);
RUN_TEST
(
test_server
);
//RUN_TEST(test_mg_connect);
//RUN_TEST(test_rewrites);
#ifdef MONGOOSE_USE_SSL
RUN_TEST
(
test_ssl
);
#endif
RUN_TEST
(
test_rewrites
);
return
NULL
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment