Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / websockets / websocket_handshake_stream_create_helper_test.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/websockets/websocket_handshake_stream_create_helper.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "net/base/completion_callback.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_request_headers.h"
13 #include "net/http/http_request_info.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/http/http_response_info.h"
16 #include "net/socket/client_socket_handle.h"
17 #include "net/socket/socket_test_util.h"
18 #include "net/websockets/websocket_basic_handshake_stream.h"
19 #include "net/websockets/websocket_stream.h"
20 #include "net/websockets/websocket_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "url/gurl.h"
23
24 namespace net {
25 namespace {
26
27 // This class encapsulates the details of creating a mock ClientSocketHandle.
28 class MockClientSocketHandleFactory {
29  public:
30   MockClientSocketHandleFactory()
31       : histograms_("a"),
32         pool_(1, 1, &histograms_, socket_factory_maker_.factory()) {}
33
34   // The created socket expects |expect_written| to be written to the socket,
35   // and will respond with |return_to_read|. The test will fail if the expected
36   // text is not written, or if all the bytes are not read.
37   scoped_ptr<ClientSocketHandle> CreateClientSocketHandle(
38       const std::string& expect_written,
39       const std::string& return_to_read) {
40     socket_factory_maker_.SetExpectations(expect_written, return_to_read);
41     scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle);
42     socket_handle->Init(
43         "a",
44         scoped_refptr<MockTransportSocketParams>(),
45         MEDIUM,
46         CompletionCallback(),
47         &pool_,
48         BoundNetLog());
49     return socket_handle.Pass();
50   }
51
52  private:
53   WebSocketDeterministicMockClientSocketFactoryMaker socket_factory_maker_;
54   ClientSocketPoolHistograms histograms_;
55   MockTransportClientSocketPool pool_;
56
57   DISALLOW_COPY_AND_ASSIGN(MockClientSocketHandleFactory);
58 };
59
60 class TestConnectDelegate : public WebSocketStream::ConnectDelegate {
61  public:
62   virtual ~TestConnectDelegate() {}
63
64   virtual void OnSuccess(scoped_ptr<WebSocketStream> stream) OVERRIDE {}
65   virtual void OnFailure(const std::string& failure_message) OVERRIDE {}
66   virtual void OnStartOpeningHandshake(
67       scoped_ptr<WebSocketHandshakeRequestInfo> request) OVERRIDE {}
68   virtual void OnFinishOpeningHandshake(
69       scoped_ptr<WebSocketHandshakeResponseInfo> response) OVERRIDE {}
70 };
71
72 class WebSocketHandshakeStreamCreateHelperTest : public ::testing::Test {
73  protected:
74   scoped_ptr<WebSocketStream> CreateAndInitializeStream(
75       const std::string& socket_url,
76       const std::string& socket_path,
77       const std::vector<std::string>& sub_protocols,
78       const std::string& origin,
79       const std::string& extra_request_headers,
80       const std::string& extra_response_headers) {
81     WebSocketHandshakeStreamCreateHelper create_helper(&connect_delegate_,
82                                                        sub_protocols);
83
84     scoped_ptr<ClientSocketHandle> socket_handle =
85         socket_handle_factory_.CreateClientSocketHandle(
86             WebSocketStandardRequest(
87                 socket_path, origin, extra_request_headers),
88             WebSocketStandardResponse(extra_response_headers));
89
90     scoped_ptr<WebSocketHandshakeStreamBase> handshake(
91         create_helper.CreateBasicStream(socket_handle.Pass(), false));
92
93     // If in future the implementation type returned by CreateBasicStream()
94     // changes, this static_cast will be wrong. However, in that case the test
95     // will fail and AddressSanitizer should identify the issue.
96     static_cast<WebSocketBasicHandshakeStream*>(handshake.get())
97         ->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ==");
98
99     HttpRequestInfo request_info;
100     request_info.url = GURL(socket_url);
101     request_info.method = "GET";
102     request_info.load_flags = LOAD_DISABLE_CACHE | LOAD_DO_NOT_PROMPT_FOR_LOGIN;
103     int rv = handshake->InitializeStream(
104         &request_info, DEFAULT_PRIORITY, BoundNetLog(), CompletionCallback());
105     EXPECT_EQ(OK, rv);
106
107     HttpRequestHeaders headers;
108     headers.SetHeader("Host", "localhost");
109     headers.SetHeader("Connection", "Upgrade");
110     headers.SetHeader("Pragma", "no-cache");
111     headers.SetHeader("Cache-Control", "no-cache");
112     headers.SetHeader("Upgrade", "websocket");
113     headers.SetHeader("Origin", origin);
114     headers.SetHeader("Sec-WebSocket-Version", "13");
115     headers.SetHeader("User-Agent", "");
116     headers.SetHeader("Accept-Encoding", "gzip,deflate");
117     headers.SetHeader("Accept-Language", "en-us,fr");
118
119     HttpResponseInfo response;
120     TestCompletionCallback dummy;
121
122     rv = handshake->SendRequest(headers, &response, dummy.callback());
123
124     EXPECT_EQ(OK, rv);
125
126     rv = handshake->ReadResponseHeaders(dummy.callback());
127     EXPECT_EQ(OK, rv);
128     EXPECT_EQ(101, response.headers->response_code());
129     EXPECT_TRUE(response.headers->HasHeaderValue("Connection", "Upgrade"));
130     EXPECT_TRUE(response.headers->HasHeaderValue("Upgrade", "websocket"));
131     return handshake->Upgrade();
132   }
133
134   MockClientSocketHandleFactory socket_handle_factory_;
135   TestConnectDelegate connect_delegate_;
136 };
137
138 // Confirm that the basic case works as expected.
139 TEST_F(WebSocketHandshakeStreamCreateHelperTest, BasicStream) {
140   scoped_ptr<WebSocketStream> stream =
141       CreateAndInitializeStream("ws://localhost/", "/",
142                                 std::vector<std::string>(), "http://localhost/",
143                                 "", "");
144   EXPECT_EQ("", stream->GetExtensions());
145   EXPECT_EQ("", stream->GetSubProtocol());
146 }
147
148 // Verify that the sub-protocols are passed through.
149 TEST_F(WebSocketHandshakeStreamCreateHelperTest, SubProtocols) {
150   std::vector<std::string> sub_protocols;
151   sub_protocols.push_back("chat");
152   sub_protocols.push_back("superchat");
153   scoped_ptr<WebSocketStream> stream =
154       CreateAndInitializeStream("ws://localhost/",
155                                 "/",
156                                 sub_protocols,
157                                 "http://localhost/",
158                                 "Sec-WebSocket-Protocol: chat, superchat\r\n",
159                                 "Sec-WebSocket-Protocol: superchat\r\n");
160   EXPECT_EQ("superchat", stream->GetSubProtocol());
161 }
162
163 // Verify that extension name is available. Bad extension names are tested in
164 // websocket_stream_test.cc.
165 TEST_F(WebSocketHandshakeStreamCreateHelperTest, Extensions) {
166   scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream(
167       "ws://localhost/",
168       "/",
169       std::vector<std::string>(),
170       "http://localhost/",
171       "",
172       "Sec-WebSocket-Extensions: permessage-deflate\r\n");
173   EXPECT_EQ("permessage-deflate", stream->GetExtensions());
174 }
175
176 // Verify that extension parameters are available. Bad parameters are tested in
177 // websocket_stream_test.cc.
178 TEST_F(WebSocketHandshakeStreamCreateHelperTest, ExtensionParameters) {
179   scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream(
180       "ws://localhost/",
181       "/",
182       std::vector<std::string>(),
183       "http://localhost/",
184       "",
185       "Sec-WebSocket-Extensions: permessage-deflate;"
186       " client_max_window_bits=14; server_max_window_bits=14;"
187       " server_no_context_takeover; client_no_context_takeover\r\n");
188
189   EXPECT_EQ(
190       "permessage-deflate;"
191       " client_max_window_bits=14; server_max_window_bits=14;"
192       " server_no_context_takeover; client_no_context_takeover",
193       stream->GetExtensions());
194 }
195
196 }  // namespace
197 }  // namespace net