Skip to content

Commit

Permalink
Improve test coverage for HTTP service
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jan 3, 2020
1 parent 646eeb0 commit 79314f7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 15 deletions.
29 changes: 18 additions & 11 deletions trunk/src/service/srs_service_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,13 @@ SrsHttpMessage::SrsHttpMessage(ISrsReader* reader, SrsFastStream* buffer) : ISrs

jsonp = false;

_method = 0;
_status = 0;
// As 0 is DELETE, so we use GET as default.
_method = SRS_CONSTS_HTTP_GET;
// 200 is ok.
_status = SRS_CONSTS_HTTP_OK;
// -1 means infinity chunked mode.
_content_length = -1;
// From HTTP/1.1, default to keep alive.
_keep_alive = true;
}

Expand Down Expand Up @@ -338,18 +342,21 @@ srs_error_t SrsHttpMessage::set_url(string url, bool allow_jsonp)

_url = url;

// use server public ip when host not specified.
// to make telnet happy.
std::string host = _header.get("Host");
if (host.empty()) {
host= srs_get_public_internet_address();
}

// parse uri from schema/server:port/path?query
std::string uri = _url;
if (!host.empty()) {
uri = "http://" + host + _url;

if (!srs_string_contains(uri, "://")) {
// use server public ip when host not specified.
// to make telnet happy.
std::string host = _header.get("Host");
if (host.empty()) {
host= srs_get_public_internet_address();
}
if (!host.empty()) {
uri = "http://" + host + _url;
}
}

if ((err = _uri->initialize(uri)) != srs_success) {
return srs_error_wrap(err, "init uri %s", uri.c_str());
}
Expand Down
70 changes: 66 additions & 4 deletions trunk/src/utest/srs_utest_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ using namespace std;
#include <srs_service_st.hpp>
#include <srs_service_utility.hpp>

// Disable coroutine test for OSX.
#if !defined(SRS_OSX)

#include <srs_service_st.hpp>
#include <srs_service_http_conn.hpp>

class MockSrsConnection : public ISrsConnection
{
public:
MockSrsConnection() {
}
virtual ~MockSrsConnection() {
}
virtual std::string remote_ip() {
return "127.0.0.1";
}
};

VOID TEST(ServiceTimeTest, TimeUnit)
{
Expand Down Expand Up @@ -376,6 +386,7 @@ VOID TEST(TCPServerTest, StringIsHex)
VOID TEST(TCPServerTest, WritevIOVC)
{
srs_error_t err;

if (true) {
MockTcpHandler h;
SrsTcpListener l(&h, _srs_tmp_host, _srs_tmp_port);
Expand Down Expand Up @@ -431,4 +442,55 @@ VOID TEST(TCPServerTest, WritevIOVC)
}
}

#endif
VOID TEST(TCPServerTest, MessageConnection)
{
srs_error_t err;

if (true) {
MockSrsConnection conn;
SrsHttpMessage m;
m.set_connection(&conn);
EXPECT_TRUE(&conn == m.connection());
}

if (true) {
SrsHttpMessage m;
HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/live/livestream.flv?callback=fn&method=POST", true));
EXPECT_TRUE(m.jsonp); EXPECT_STREQ("POST", m.jsonp_method.c_str());
}

if (true) {
SrsHttpMessage m;
HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/live/livestream.flv?callback=fn&method=GET", true));
EXPECT_EQ(SRS_CONSTS_HTTP_GET, m.method()); EXPECT_STREQ("GET", m.method_str().c_str());
}

if (true) {
SrsHttpMessage m;
HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/live/livestream.flv?callback=fn&method=PUT", true));
EXPECT_EQ(SRS_CONSTS_HTTP_PUT, m.method()); EXPECT_STREQ("PUT", m.method_str().c_str());
}

if (true) {
SrsHttpMessage m;
HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/live/livestream.flv?callback=fn&method=POST", true));
EXPECT_EQ(SRS_CONSTS_HTTP_POST, m.method()); EXPECT_STREQ("POST", m.method_str().c_str());
}

if (true) {
SrsHttpMessage m;
HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/live/livestream.flv?callback=fn&method=DELETE", true));
EXPECT_EQ(SRS_CONSTS_HTTP_DELETE, m.method()); EXPECT_STREQ("DELETE", m.method_str().c_str());
}

if (true) {
SrsHttpMessage m;
m.set_basic(100, 0, 0); EXPECT_STREQ("OTHER", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_GET, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_GET, m.method()); EXPECT_STREQ("GET", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_PUT, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_PUT, m.method()); EXPECT_STREQ("PUT", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_POST, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_POST, m.method()); EXPECT_STREQ("POST", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_DELETE, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_DELETE, m.method()); EXPECT_STREQ("DELETE", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_OPTIONS, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_OPTIONS, m.method()); EXPECT_STREQ("OPTIONS", m.method_str().c_str());
}
}

0 comments on commit 79314f7

Please sign in to comment.