From ec0fb82c0e511730d04a0b790f5da83c7256e553 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 17 Dec 2019 19:39:36 +0800 Subject: [PATCH] Improve test coverage for http server mux. --- trunk/src/protocol/srs_http_stack.cpp | 2 +- trunk/src/utest/srs_utest_http.cpp | 208 +++++++++++++++++++++++++- 2 files changed, 208 insertions(+), 2 deletions(-) diff --git a/trunk/src/protocol/srs_http_stack.cpp b/trunk/src/protocol/srs_http_stack.cpp index 5d14de93a..67d7e6e0a 100644 --- a/trunk/src/protocol/srs_http_stack.cpp +++ b/trunk/src/protocol/srs_http_stack.cpp @@ -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::iterator it; for (it = hijackers.begin(); it != hijackers.end(); ++it) { ISrsHttpMatchHijacker* hijacker = *it; diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index 9417ff4ad..d70750ed2 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -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());