Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 "buffet/data_encoding.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 9 | using namespace buffet::data_encoding; // NOLINT(build/namespaces) |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 10 | |
| 11 | TEST(data_encoding, UrlEncoding) { |
| 12 | std::string test = "\"http://sample/path/0014.html \""; |
| 13 | std::string encoded = UrlEncode(test.c_str()); |
| 14 | EXPECT_EQ("%22http%3A%2F%2Fsample%2Fpath%2F0014.html+%22", |
| 15 | encoded); |
| 16 | EXPECT_EQ(test, UrlDecode(encoded.c_str())); |
| 17 | |
| 18 | test = "\"http://sample/path/0014.html \""; |
| 19 | encoded = UrlEncode(test.c_str(), false); |
| 20 | EXPECT_EQ("%22http%3A%2F%2Fsample%2Fpath%2F0014.html%20%22", |
| 21 | encoded); |
| 22 | EXPECT_EQ(test, UrlDecode(encoded.c_str())); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | TEST(data_encoding, WebParamsEncoding) { |
| 26 | std::string encoded = WebParamsEncode({{"q", "test"}, |
| 27 | {"path", "/usr/bin"}, |
| 28 | {"#", "%"}}); |
| 29 | EXPECT_EQ("q=test&path=%2Fusr%2Fbin&%23=%25", encoded); |
| 30 | |
| 31 | auto params = WebParamsDecode(encoded); |
| 32 | EXPECT_EQ(3, params.size()); |
| 33 | EXPECT_EQ("q", params[0].first); |
| 34 | EXPECT_EQ("test", params[0].second); |
| 35 | EXPECT_EQ("path", params[1].first); |
| 36 | EXPECT_EQ("/usr/bin", params[1].second); |
| 37 | EXPECT_EQ("#", params[2].first); |
| 38 | EXPECT_EQ("%", params[2].second); |
| 39 | } |