Skip to content

Commit

Permalink
improve utest
Browse files Browse the repository at this point in the history
  • Loading branch information
duiniuluantanqin committed Jan 1, 2023
1 parent b28e685 commit 90abe17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 20 additions & 2 deletions trunk/src/utest/srs_utest_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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"
Expand All @@ -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);
}
}

Expand Down
4 changes: 4 additions & 0 deletions trunk/src/utest/srs_utest_http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand All @@ -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())

Expand Down

0 comments on commit 90abe17

Please sign in to comment.