Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
b7d410e1
Commit
b7d410e1
authored
5 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
tests: add http tester
parent
77a377a4
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/httptester.cpp
+70
-35
70 additions, 35 deletions
tests/httptester.cpp
tests/httptester.h
+5
-8
5 additions, 8 deletions
tests/httptester.h
with
75 additions
and
43 deletions
tests/httptester.cpp
+
70
−
35
View file @
b7d410e1
...
@@ -19,41 +19,35 @@
...
@@ -19,41 +19,35 @@
#include
"httptester.h"
#include
"httptester.h"
// std
#include
<opendht/value.h>
#include
<opendht/dhtrunner.h>
#include
<iostream>
#include
<iostream>
#include
<string>
#include
<string>
#include
<chrono>
#include
<chrono>
#include
<condition_variable>
#include
<condition_variable>
namespace
test
{
namespace
test
{
CPPUNIT_TEST_SUITE_REGISTRATION
(
HttpTester
);
CPPUNIT_TEST_SUITE_REGISTRATION
(
HttpTester
);
void
void
HttpTester
::
setUp
()
{
HttpTester
::
setUp
()
{
logger
=
dht
::
log
::
getStdLogger
();
nodePeer
=
std
::
make_shared
<
dht
::
DhtRunner
>
();
nodePeer
->
run
(
0
);
nodePeer
.
run
(
0
,
/*identity*/
{},
/*threaded*/
true
);
nodeProxy
=
std
::
make_shared
<
dht
::
DhtRunner
>
();
auto
nodeProxy
=
std
::
make_shared
<
dht
::
DhtRunner
>
();
nodeProxy
->
run
(
0
,
/*identity*/
{},
/*threaded*/
true
);
nodeProxy
->
run
(
0
,
/*identity*/
{},
/*threaded*/
true
);
nodeProxy
->
bootstrap
(
nodePeer
.
getBound
());
nodeProxy
->
bootstrap
(
nodePeer
->
getBound
());
serverProxy
=
std
::
unique_ptr
<
dht
::
DhtProxyServer
>
(
serverProxy
=
std
::
unique_ptr
<
dht
::
DhtProxyServer
>
(
new
dht
::
DhtProxyServer
(
new
dht
::
DhtProxyServer
(
/*http*/
dht
::
crypto
::
Identity
{},
nodeProxy
,
8080
,
/*pushServer*/
"127.0.0.1:8090"
,
logger
));
/*http*/
dht
::
crypto
::
Identity
{},
nodeProxy
,
8080
,
/*pushServer*/
"127.0.0.1:8090"
));
}
}
void
void
HttpTester
::
tearDown
()
{
HttpTester
::
tearDown
()
{
logger
->
d
(
"[tester:http] stopping peer node"
);
serverProxy
.
reset
();
nodePeer
.
join
();
nodePeer
->
join
();
logger
->
d
(
"[tester:http] stopping proxy server"
);
serverProxy
.
reset
(
nullptr
);
logger
->
d
(
"[tester:http] stopping proxy node"
);
nodeProxy
->
join
();
}
}
void
void
...
@@ -234,31 +228,72 @@ HttpTester::test_send_json() {
...
@@ -234,31 +228,72 @@ HttpTester::test_send_json() {
std
::
mutex
cv_m
;
std
::
mutex
cv_m
;
std
::
unique_lock
<
std
::
mutex
>
lk
(
cv_m
);
std
::
unique_lock
<
std
::
mutex
>
lk
(
cv_m
);
bool
done
=
false
;
bool
done
=
false
;
unsigned
int
status
=
0
;
dht
::
Value
val
{
"hey"
};
auto
json
=
dht
::
Value
(
"hey"
).
toJson
();
auto
json
=
val
.
toJson
();
json
[
"permanent"
]
=
false
;
std
::
cout
<<
"[test_send_json] sending:
\n
"
<<
json
<<
std
::
endl
;
Json
::
Value
resp_val
;
Json
::
Value
resp_val
;
unsigned
int
status
=
0
;
std
::
string
url
=
"http://127.0.0.1:8080/key"
;
// Act
// Act
auto
request
=
std
::
make_shared
<
dht
::
http
::
Request
>
(
serverProxy
->
io_context
(),
url
,
json
,
auto
request
=
std
::
make_shared
<
dht
::
http
::
Request
>
(
serverProxy
->
io_context
(),
[
this
,
&
cv
,
&
done
,
&
status
,
&
resp_val
](
Json
::
Value
value
,
unsigned
int
status_code
){
"http://127.0.0.1:8080/key"
,
if
(
status_code
!=
200
and
logger
)
json
,
logger
->
e
(
"[tester] [status] failed with code=%i"
,
status_code
)
;
[
&
](
Json
::
Value
value
,
unsigned
int
status_code
)
{
std
::
cout
<<
"[tester] got response:
\n
"
<<
value
<<
std
::
endl
;
std
::
lock_guard
<
std
::
mutex
>
lk
(
cv_m
)
;
resp_val
=
value
;
resp_val
=
std
::
move
(
value
)
;
status
=
status_code
;
status
=
status_code
;
done
=
true
;
done
=
true
;
cv
.
notify_all
();
cv
.
notify_all
();
},
logger
);
});
request
->
set_method
(
restinio
::
http_method_post
());
request
->
send
();
request
->
send
();
// Assert
// Assert
CPPUNIT_ASSERT
(
cv
.
wait_for
(
lk
,
std
::
chrono
::
seconds
(
10
),
[
&
]{
return
done
;
}));
CPPUNIT_ASSERT
(
cv
.
wait_for
(
lk
,
std
::
chrono
::
seconds
(
10
),
[
&
]{
return
done
;
}));
CPPUNIT_ASSERT
(
status
==
200
);
CPPUNIT_ASSERT_EQUAL
(
200u
,
status
);
CPPUNIT_ASSERT
(
resp_val
[
"data"
]
==
val
.
toJson
()[
"data"
]);
CPPUNIT_ASSERT_EQUAL
(
json
[
"data"
].
asString
(),
resp_val
[
"data"
].
asString
());
done
=
false
;
#if 0
request = std::make_shared<dht::http::Request>(serverProxy->io_context(),
"http://google.ca",
[&](const dht::http::Response& response) {
std::lock_guard<std::mutex> lk(cv_m);
status = response.status_code;
done = true;
cv.notify_all();
});
request->send();
CPPUNIT_ASSERT(cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; }));
CPPUNIT_ASSERT(status != 0);
done = false;
request = std::make_shared<dht::http::Request>(serverProxy->io_context(),
"https://google.ca",
[&](const dht::http::Response& response) {
std::lock_guard<std::mutex> lk(cv_m);
status = response.status_code;
done = true;
cv.notify_all();
});
request->send();
CPPUNIT_ASSERT(cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; }));
CPPUNIT_ASSERT(status != 0);
done = false;
request = std::make_shared<dht::http::Request>(serverProxy->io_context(),
"https://google.ca/sdbjklwGBIP",
[&](const dht::http::Response& response) {
std::lock_guard<std::mutex> lk(cv_m);
status = response.status_code;
done = true;
cv.notify_all();
});
request->send();
CPPUNIT_ASSERT(cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; }));
CPPUNIT_ASSERT_EQUAL(404u, status);
#endif
}
}
}
// namespace test
}
// namespace test
This diff is collapsed.
Click to expand it.
tests/httptester.h
+
5
−
8
View file @
b7d410e1
...
@@ -23,12 +23,12 @@
...
@@ -23,12 +23,12 @@
#include
<cppunit/TestFixture.h>
#include
<cppunit/TestFixture.h>
#include
<cppunit/extensions/HelperMacros.h>
#include
<cppunit/extensions/HelperMacros.h>
#include
<opendht/value.h>
#include
<opendht/log.h>
#include
<opendht/http.h>
#include
<opendht/http.h>
#include
<opendht/dhtrunner.h>
#include
<opendht/dht_proxy_server.h>
#include
<opendht/dht_proxy_server.h>
#include
<asio.hpp>
#include
<thread>
#include
<memory>
namespace
test
{
namespace
test
{
...
@@ -88,10 +88,7 @@ class HttpTester : public CppUnit::TestFixture {
...
@@ -88,10 +88,7 @@ class HttpTester : public CppUnit::TestFixture {
void
test_send_json
();
void
test_send_json
();
private:
private:
std
::
shared_ptr
<
dht
::
Logger
>
logger
{};
std
::
shared_ptr
<
dht
::
DhtRunner
>
nodePeer
;
dht
::
DhtRunner
nodePeer
;
std
::
shared_ptr
<
dht
::
DhtRunner
>
nodeProxy
;
std
::
unique_ptr
<
dht
::
DhtProxyServer
>
serverProxy
;
std
::
unique_ptr
<
dht
::
DhtProxyServer
>
serverProxy
;
};
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment