1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
24a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
34a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// found in the LICENSE file.
44a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
54a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include <string>
64a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
7ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/ref_counted.h"
8ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h"
94a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "base/values.h"
104a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "net/websockets/websocket_net_log_params.h"
114a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "testing/gtest/include/gtest/gtest.h"
124a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
134a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben MurdochTEST(NetLogWebSocketHandshakeParameterTest, ToValue) {
144a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  ListValue* list = new ListValue();
154a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("GET /demo HTTP/1.1"));
164a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("Host: example.com"));
174a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("Connection: Upgrade"));
184a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("Sec-WebSocket-Key2: 12998 5 Y3 1  .P00"));
194a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("Sec-WebSocket-Protocol: sample"));
204a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("Upgrade: WebSocket"));
214a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5"));
224a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("Origin: http://example.com"));
234a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue(""));
244a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  list->Append(new StringValue("\\x00\\x01\\x0a\\x0d\\xff\\xfe\\x0d\\x0a"));
254a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
264a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  DictionaryValue expected;
274a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  expected.Set("headers", list);
284a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
294a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  const std::string key("\x00\x01\x0a\x0d\xff\xfe\x0d\x0a", 8);
304a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  const std::string testInput =
314a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "GET /demo HTTP/1.1\r\n"
324a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "Host: example.com\r\n"
334a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "Connection: Upgrade\r\n"
344a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "Sec-WebSocket-Key2: 12998 5 Y3 1  .P00\r\n"
354a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "Sec-WebSocket-Protocol: sample\r\n"
364a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "Upgrade: WebSocket\r\n"
374a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5\r\n"
384a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "Origin: http://example.com\r\n"
394a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    "\r\n" +
404a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    key;
414a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
424a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  scoped_refptr<net::NetLogWebSocketHandshakeParameter> parameter(
434a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch      new net::NetLogWebSocketHandshakeParameter(testInput));
444a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  scoped_ptr<Value> actual(parameter->ToValue());
454a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
464a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  EXPECT_TRUE(expected.Equals(actual.get()));
474a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch}
48