1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-14 20:31:56 +00:00

Improve test coverage for http server mux.

This commit is contained in:
winlin 2019-12-17 19:39:36 +08:00
parent 81947df819
commit ec0fb82c0e
2 changed files with 208 additions and 2 deletions

View file

@ -724,7 +724,7 @@ srs_error_t SrsHttpServeMux::find_handler(ISrsHttpMessage* r, ISrsHttpHandler**
// always hijack.
if (!hijackers.empty()) {
// notice all hijacker the match failed.
// notify all hijackers unless matching failed.
std::vector<ISrsHttpMatchHijacker*>::iterator it;
for (it = hijackers.begin(); it != hijackers.end(); ++it) {
ISrsHttpMatchHijacker* hijacker = *it;

View file

@ -132,7 +132,7 @@ public:
}
};
class MockHttpHandler : public ISrsHttpHandler
class MockHttpHandler : virtual public ISrsHttpHandler, virtual public ISrsHttpMatchHijacker
{
public:
string bytes;
@ -144,6 +144,12 @@ public:
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* /*r*/) {
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*/)
@ -318,6 +324,206 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxer)
{
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) {
SrsHttpServeMux s;
HELPER_ASSERT_SUCCESS(s.initialize());