1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Refactor HttpResponseWriter.write, default to single text mode.

This commit is contained in:
winlin 2019-12-17 15:14:59 +08:00
parent 8cdb7cc727
commit d9842b0371
6 changed files with 96 additions and 24 deletions

View file

@ -108,6 +108,47 @@ string mock_http_response(int status, string content)
return ss.str();
}
class MockFileReaderFactory : public ISrsFileReaderFactory
{
public:
string bytes;
MockFileReaderFactory(string data) {
bytes = data;
}
virtual ~MockFileReaderFactory() {
}
virtual SrsFileReader* create_file_reader() {
return new MockSrsFileReader((const char*)bytes.data(), (int)bytes.length());
}
};
class MockHttpHandler : public ISrsHttpHandler
{
public:
string bytes;
MockHttpHandler(string data) {
bytes = data;
}
virtual ~MockHttpHandler() {
}
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* /*r*/) {
return w->write((char*)bytes.data(), (int)bytes.length());
}
};
bool _mock_srs_path_always_exists(std::string /*path*/)
{
return true;
}
bool _mock_srs_path_not_exists(std::string /*path*/)
{
return false;
}
#define __MOCK_HTTP_EXPECT_STREQ(status, text, w) \
EXPECT_STREQ(mock_http_response(status, text).c_str(), HELPER_BUFFER2STR(&w.io.out_buffer).c_str())
VOID TEST(ProtocolHTTPTest, StatusCode2Text)
{
EXPECT_STREQ(SRS_CONSTS_HTTP_OK_str, srs_generate_http_status_text(SRS_CONSTS_HTTP_OK).c_str());
@ -123,10 +164,20 @@ VOID TEST(ProtocolHTTPTest, StatusCode2Text)
VOID TEST(ProtocolHTTPTest, ResponseDetect)
{
EXPECT_STREQ("application/octet-stream", srs_go_http_detect(NULL, 0).c_str());
EXPECT_STREQ("application/octet-stream", srs_go_http_detect((char*)"Hello, world!", 0).c_str());
}
#define __MOCK_HTTP_EXPECT_STREQ(status, text, w) \
EXPECT_STREQ(mock_http_response(status, text).c_str(), HELPER_BUFFER2STR(&w.io.out_buffer).c_str())
VOID TEST(ProtocolHTTPTest, ResponseWriter)
{
if (true) {
MockResponseWriter w;
char msg[] = "Hello, world!";
w.write((char*)msg, sizeof(msg) - 1);
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
}
}
VOID TEST(ProtocolHTTPTest, ResponseHTTPError)
{
@ -137,6 +188,18 @@ VOID TEST(ProtocolHTTPTest, ResponseHTTPError)
HELPER_EXPECT_SUCCESS(srs_go_http_error(&w, SRS_CONSTS_HTTP_Found));
__MOCK_HTTP_EXPECT_STREQ(302, "Found", w);
}
if (true) {
MockResponseWriter w;
HELPER_EXPECT_SUCCESS(srs_go_http_error(&w, SRS_CONSTS_HTTP_InternalServerError));
__MOCK_HTTP_EXPECT_STREQ(500, "Internal Server Error", w);
}
if (true) {
MockResponseWriter w;
HELPER_EXPECT_SUCCESS(srs_go_http_error(&w, SRS_CONSTS_HTTP_ServiceUnavailable));
__MOCK_HTTP_EXPECT_STREQ(503, "Service Unavailable", w);
}
}
VOID TEST(ProtocolHTTPTest, HTTPHeader)
@ -173,28 +236,24 @@ VOID TEST(ProtocolHTTPTest, HTTPHeader)
srs_freep(o);
}
class MockFileReaderFactory : public ISrsFileReaderFactory
VOID TEST(ProtocolHTTPTest, HTTPServerMuxer)
{
public:
string bytes;
MockFileReaderFactory(string data) {
bytes = data;
}
virtual ~MockFileReaderFactory() {
}
virtual SrsFileReader* create_file_reader() {
return new MockSrsFileReader((const char*)bytes.data(), (int)bytes.length());
}
};
srs_error_t err;
bool _mock_srs_path_always_exists(std::string /*path*/)
{
return true;
}
if (true) {
SrsHttpServeMux s;
HELPER_ASSERT_SUCCESS(s.initialize());
bool _mock_srs_path_not_exists(std::string /*path*/)
{
return false;
MockHttpHandler* hroot = new MockHttpHandler("Hello, world!");
HELPER_ASSERT_SUCCESS(s.handle("/", 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);
}
}
VOID TEST(ProtocolHTTPTest, VodStreamHandlers)