From 90abe1726ef8a6d41f679278413f5dff82c65165 Mon Sep 17 00:00:00 2001 From: duiniuluantanqin <495810242@qq.com> Date: Sun, 1 Jan 2023 16:19:18 +0800 Subject: [PATCH] improve utest --- trunk/src/app/srs_app_http_static.cpp | 2 +- trunk/src/utest/srs_utest_http.cpp | 22 ++++++++++++++++++++-- trunk/src/utest/srs_utest_http.hpp | 4 ++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index c04a00a93b..8b80b53e1d 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -88,7 +88,7 @@ srs_error_t SrsHlsStream::serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMess *served = true; // Already exists context, response with rebuilt m3u8 content. - if (!ctx.empty()) { + if (!ctx.empty() && ctx_is_exist(ctx)) { // If HLS stream is disabled, use SrsHttpFileServer to serve HLS, which is normal file server. if (!_srs_config->get_hls_ts_ctx_enabled(req->vhost)) { *served = false; diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index 967586183d..33099852b7 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -144,6 +144,16 @@ string mock_http_response3(int status, string content) return ss.str(); } +string mock_http_response4(int status, string content) +{ + stringstream ss; + ss << "HTTP/1.1 " << status << " " << srs_generate_http_status_text(status) << "\r\n" + << "Content-Length: " << content.length() + 58 << "\r\n\r\n" + << "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1\n" // length is 58 + << content; + return ss.str(); +} + bool is_string_contain(string substr, string str) { return (string::npos != str.find(substr)); @@ -1286,7 +1296,11 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers) HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?hls_ctx=123456", false)); HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r)); - __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); + __MOCK_HTTP_EXPECT_STREQ4(200, "/index.m3u8?hls_ctx=123456", w); + + MockResponseWriter w2; + HELPER_ASSERT_SUCCESS(h.serve_http(&w2, &r)); + __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w2); } // Should return "hls_ctx" @@ -1304,7 +1318,11 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers) HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?hls_ctx=123456", false)); HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r)); - __MOCK_HTTP_EXPECT_STREQ(200, "livestream-13.ts?hls_ctx=123456", w); + __MOCK_HTTP_EXPECT_STREQ4(200, "/index.m3u8?hls_ctx=123456", w); + + MockResponseWriter w2; + HELPER_ASSERT_SUCCESS(h.serve_http(&w2, &r)); + __MOCK_HTTP_EXPECT_STREQ(200, "livestream-13.ts?hls_ctx=123456", w2); } } diff --git a/trunk/src/utest/srs_utest_http.hpp b/trunk/src/utest/srs_utest_http.hpp index 0ded27178b..06b1e4c966 100644 --- a/trunk/src/utest/srs_utest_http.hpp +++ b/trunk/src/utest/srs_utest_http.hpp @@ -51,6 +51,7 @@ class MockMSegmentsReader : public ISrsReader string mock_http_response(int status, string content); string mock_http_response2(int status, string content); +string mock_http_response4(int status, string content); bool is_string_contain(string substr, string str); #define __MOCK_HTTP_EXPECT_STREQ(status, text, w) \ @@ -59,6 +60,9 @@ bool is_string_contain(string substr, string str); #define __MOCK_HTTP_EXPECT_STREQ2(status, text, w) \ EXPECT_STREQ(mock_http_response2(status, text).c_str(), HELPER_BUFFER2STR(&w.io.out_buffer).c_str()) +#define __MOCK_HTTP_EXPECT_STREQ4(status, text, w) \ + EXPECT_STREQ(mock_http_response4(status, text).c_str(), HELPER_BUFFER2STR(&w.io.out_buffer).c_str()) + #define __MOCK_HTTP_EXPECT_STRHAS(status, text, w) \ EXPECT_PRED2(is_string_contain, text, HELPER_BUFFER2STR(&w.io.out_buffer).c_str())