mirror of
https://github.com/ossrs/srs.git
synced 2025-02-15 04:42:04 +00:00
Improve test coverage for http server mux.
This commit is contained in:
parent
81947df819
commit
ec0fb82c0e
2 changed files with 208 additions and 2 deletions
|
@ -724,7 +724,7 @@ srs_error_t SrsHttpServeMux::find_handler(ISrsHttpMessage* r, ISrsHttpHandler**
|
||||||
|
|
||||||
// always hijack.
|
// always hijack.
|
||||||
if (!hijackers.empty()) {
|
if (!hijackers.empty()) {
|
||||||
// notice all hijacker the match failed.
|
// notify all hijackers unless matching failed.
|
||||||
std::vector<ISrsHttpMatchHijacker*>::iterator it;
|
std::vector<ISrsHttpMatchHijacker*>::iterator it;
|
||||||
for (it = hijackers.begin(); it != hijackers.end(); ++it) {
|
for (it = hijackers.begin(); it != hijackers.end(); ++it) {
|
||||||
ISrsHttpMatchHijacker* hijacker = *it;
|
ISrsHttpMatchHijacker* hijacker = *it;
|
||||||
|
|
|
@ -132,7 +132,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class MockHttpHandler : public ISrsHttpHandler
|
class MockHttpHandler : virtual public ISrsHttpHandler, virtual public ISrsHttpMatchHijacker
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
string bytes;
|
string bytes;
|
||||||
|
@ -144,6 +144,12 @@ public:
|
||||||
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* /*r*/) {
|
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* /*r*/) {
|
||||||
return w->write((char*)bytes.data(), (int)bytes.length());
|
return w->write((char*)bytes.data(), (int)bytes.length());
|
||||||
}
|
}
|
||||||
|
virtual srs_error_t hijack(ISrsHttpMessage* /*r*/, ISrsHttpHandler** ph) {
|
||||||
|
if (ph) {
|
||||||
|
*ph = this;
|
||||||
|
}
|
||||||
|
return srs_success;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bool _mock_srs_path_always_exists(std::string /*path*/)
|
bool _mock_srs_path_always_exists(std::string /*path*/)
|
||||||
|
@ -318,6 +324,206 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxer)
|
||||||
{
|
{
|
||||||
srs_error_t err;
|
srs_error_t err;
|
||||||
|
|
||||||
|
// For hijacker, notify all.
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler h1("Done");
|
||||||
|
s.hijack(&h1);
|
||||||
|
|
||||||
|
MockHttpHandler h0("Hello, world!");
|
||||||
|
s.hijack(&h0);
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/home/index.html", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
// For hijacker, notify all.
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler h0("Hello, world!");
|
||||||
|
s.hijack(&h0);
|
||||||
|
|
||||||
|
MockHttpHandler h1("Done");
|
||||||
|
s.hijack(&h1);
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/home/index.html", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Done", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If not endswith '/', exactly match.
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler* h0 = new MockHttpHandler("Hello, world!");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/", h0));
|
||||||
|
|
||||||
|
MockHttpHandler* h1 = new MockHttpHandler("Done");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/home", h1));
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/home/index.html", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If endswith '/', match any.
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler* h0 = new MockHttpHandler("Hello, world!");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/", h0));
|
||||||
|
|
||||||
|
MockHttpHandler* h1 = new MockHttpHandler("Done");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/home/", h1));
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/home/index.html", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Done", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler* h0 = new MockHttpHandler("Hello, world!");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/", h0));
|
||||||
|
|
||||||
|
MockHttpHandler* h1 = new MockHttpHandler("Done");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/api/v1", h1));
|
||||||
|
|
||||||
|
MockHttpHandler* h2 = new MockHttpHandler("Another one");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/apis/", h2));
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/apis/v1/echo", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Another one", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler* h0 = new MockHttpHandler("Hello, world!");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/", h0));
|
||||||
|
|
||||||
|
MockHttpHandler* h1 = new MockHttpHandler("Done");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/api/v1/", h1));
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/apis/v1/echo", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler* h0 = new MockHttpHandler("Hello, world!");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/api/", h0));
|
||||||
|
|
||||||
|
MockHttpHandler* h1 = new MockHttpHandler("Done");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/api/v1/", h1));
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/api/v1/echo", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Done", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler* h0 = new MockHttpHandler("Hello, world!");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/api/", h0));
|
||||||
|
|
||||||
|
MockHttpHandler* h1 = new MockHttpHandler("Done");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/apis/", h1));
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/apis/v1/echo", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Done", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler* h0 = new MockHttpHandler("Hello, world!");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/", h0));
|
||||||
|
|
||||||
|
MockHttpHandler* h1 = new MockHttpHandler("Done");
|
||||||
|
HELPER_ASSERT_SUCCESS(s.handle("/index.html", h1));
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Done", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler hroot("Hello, world!");
|
||||||
|
s.hijack(&hroot);
|
||||||
|
s.unhijack(&hroot);
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(404, "Not Found", w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
SrsHttpServeMux s;
|
||||||
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
||||||
|
MockHttpHandler hroot("Hello, world!");
|
||||||
|
s.hijack(&hroot);
|
||||||
|
|
||||||
|
MockResponseWriter w;
|
||||||
|
SrsHttpMessage r(NULL, NULL);
|
||||||
|
HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false));
|
||||||
|
|
||||||
|
HELPER_ASSERT_SUCCESS(s.serve_http(&w, &r));
|
||||||
|
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
|
||||||
|
}
|
||||||
|
|
||||||
if (true) {
|
if (true) {
|
||||||
SrsHttpServeMux s;
|
SrsHttpServeMux s;
|
||||||
HELPER_ASSERT_SUCCESS(s.initialize());
|
HELPER_ASSERT_SUCCESS(s.initialize());
|
||||||
|
|
Loading…
Reference in a new issue