2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #include "webrtc/base/asynchttprequest.h"
13 #include "webrtc/base/gunit.h"
14 #include "webrtc/base/httpserver.h"
15 #include "webrtc/base/socketstream.h"
16 #include "webrtc/base/thread.h"
20 static const SocketAddress kServerAddr("127.0.0.1", 0);
21 static const SocketAddress kServerHostnameAddr("localhost", 0);
22 static const char kServerGetPath[] = "/get";
23 static const char kServerPostPath[] = "/post";
24 static const char kServerResponse[] = "This is a test";
26 class TestHttpServer : public HttpServer, public sigslot::has_slots<> {
28 TestHttpServer(Thread* thread, const SocketAddress& addr) :
29 socket_(thread->socketserver()->CreateAsyncSocket(addr.family(),
33 socket_->SignalReadEvent.connect(this, &TestHttpServer::OnAccept);
36 SocketAddress address() const { return socket_->GetLocalAddress(); }
37 void Close() const { socket_->Close(); }
40 void OnAccept(AsyncSocket* socket) {
41 AsyncSocket* new_socket = socket_->Accept(NULL);
43 HandleConnection(new SocketStream(new_socket));
46 rtc::scoped_ptr<AsyncSocket> socket_;
49 class AsyncHttpRequestTest : public testing::Test,
50 public sigslot::has_slots<> {
52 AsyncHttpRequestTest()
55 server_(Thread::Current(), kServerAddr) {
56 server_.SignalHttpRequest.connect(this, &AsyncHttpRequestTest::OnRequest);
59 bool started() const { return started_; }
60 bool done() const { return done_; }
62 AsyncHttpRequest* CreateGetRequest(const std::string& host, int port,
63 const std::string& path) {
64 rtc::AsyncHttpRequest* request =
65 new rtc::AsyncHttpRequest("unittest");
66 request->SignalWorkDone.connect(this,
67 &AsyncHttpRequestTest::OnRequestDone);
68 request->request().verb = rtc::HV_GET;
69 request->set_host(host);
70 request->set_port(port);
71 request->request().path = path;
72 request->response().document.reset(new MemoryStream());
75 AsyncHttpRequest* CreatePostRequest(const std::string& host, int port,
76 const std::string& path,
77 const std::string content_type,
78 StreamInterface* content) {
79 rtc::AsyncHttpRequest* request =
80 new rtc::AsyncHttpRequest("unittest");
81 request->SignalWorkDone.connect(this,
82 &AsyncHttpRequestTest::OnRequestDone);
83 request->request().verb = rtc::HV_POST;
84 request->set_host(host);
85 request->set_port(port);
86 request->request().path = path;
87 request->request().setContent(content_type, content);
88 request->response().document.reset(new MemoryStream());
92 const TestHttpServer& server() const { return server_; }
95 void OnRequest(HttpServer* server, HttpServerTransaction* t) {
98 if (t->request.path == kServerGetPath) {
99 t->response.set_success("text/plain", new MemoryStream(kServerResponse));
100 } else if (t->request.path == kServerPostPath) {
101 // reverse the data and reply
103 StreamInterface* in = t->request.document.get();
104 StreamInterface* out = new MemoryStream();
106 for (size_t i = 0; i < size; ++i) {
108 in->SetPosition(size - i - 1);
109 in->Read(&ch, 1, NULL, NULL);
110 out->Write(&ch, 1, NULL, NULL);
113 t->response.set_success("text/plain", out);
115 t->response.set_error(404);
119 void OnRequestDone(SignalThread* thread) {
126 TestHttpServer server_;
129 TEST_F(AsyncHttpRequestTest, TestGetSuccess) {
130 AsyncHttpRequest* req = CreateGetRequest(
131 kServerHostnameAddr.hostname(), server().address().port(),
133 EXPECT_FALSE(started());
135 EXPECT_TRUE_WAIT(started(), 5000); // Should have started by now.
136 EXPECT_TRUE_WAIT(done(), 5000);
137 std::string response;
138 EXPECT_EQ(200U, req->response().scode);
139 ASSERT_TRUE(req->response().document);
140 req->response().document->Rewind();
141 req->response().document->ReadLine(&response);
142 EXPECT_EQ(kServerResponse, response);
146 TEST_F(AsyncHttpRequestTest, TestGetNotFound) {
147 AsyncHttpRequest* req = CreateGetRequest(
148 kServerHostnameAddr.hostname(), server().address().port(),
151 EXPECT_TRUE_WAIT(done(), 5000);
153 EXPECT_EQ(404U, req->response().scode);
154 ASSERT_TRUE(req->response().document);
155 req->response().document->GetSize(&size);
160 TEST_F(AsyncHttpRequestTest, TestGetToNonServer) {
161 AsyncHttpRequest* req = CreateGetRequest(
162 "127.0.0.1", server().address().port(),
164 // Stop the server before we send the request.
167 EXPECT_TRUE_WAIT(done(), 10000);
169 EXPECT_EQ(500U, req->response().scode);
170 ASSERT_TRUE(req->response().document);
171 req->response().document->GetSize(&size);
176 TEST_F(AsyncHttpRequestTest, DISABLED_TestGetToInvalidHostname) {
177 AsyncHttpRequest* req = CreateGetRequest(
178 "invalid", server().address().port(),
181 EXPECT_TRUE_WAIT(done(), 5000);
183 EXPECT_EQ(500U, req->response().scode);
184 ASSERT_TRUE(req->response().document);
185 req->response().document->GetSize(&size);
190 TEST_F(AsyncHttpRequestTest, TestPostSuccess) {
191 AsyncHttpRequest* req = CreatePostRequest(
192 kServerHostnameAddr.hostname(), server().address().port(),
193 kServerPostPath, "text/plain", new MemoryStream("abcd1234"));
195 EXPECT_TRUE_WAIT(done(), 5000);
196 std::string response;
197 EXPECT_EQ(200U, req->response().scode);
198 ASSERT_TRUE(req->response().document);
199 req->response().document->Rewind();
200 req->response().document->ReadLine(&response);
201 EXPECT_EQ("4321dcba", response);
205 // Ensure that we shut down properly even if work is outstanding.
206 TEST_F(AsyncHttpRequestTest, TestCancel) {
207 AsyncHttpRequest* req = CreateGetRequest(
208 kServerHostnameAddr.hostname(), server().address().port(),
214 TEST_F(AsyncHttpRequestTest, TestGetSuccessDelay) {
215 AsyncHttpRequest* req = CreateGetRequest(
216 kServerHostnameAddr.hostname(), server().address().port(),
218 req->set_start_delay(10); // Delay 10ms.
221 EXPECT_FALSE(started()); // Should not have started immediately.
222 EXPECT_TRUE_WAIT(started(), 5000); // Should have started by now.
223 EXPECT_TRUE_WAIT(done(), 5000);
224 std::string response;
225 EXPECT_EQ(200U, req->response().scode);
226 ASSERT_TRUE(req->response().document);
227 req->response().document->Rewind();
228 req->response().document->ReadLine(&response);
229 EXPECT_EQ(kServerResponse, response);