blob: d1df6d108741c2703131b3127a54d7a450a0c7dc [file] [log] [blame]
Nan Zhou93b443d2022-06-21 17:46:51 +00001#include "http_utility.hpp"
George Liu647b3cd2021-07-05 12:43:56 +08002
Nan Zhoud5c80ad2022-07-11 01:16:31 +00003#include <gtest/gtest.h> // IWYU pragma: keep
4
5// IWYU pragma: no_include <gtest/gtest-message.h>
6// IWYU pragma: no_include <gtest/gtest-test-part.h>
7// IWYU pragma: no_include "gtest/gtest_pred_impl.h"
George Liu647b3cd2021-07-05 12:43:56 +08008
Nan Zhouad3edd42022-06-21 17:46:44 +00009namespace http_helpers
10{
11namespace
12{
13
Ed Tanous99351cd2022-08-07 16:42:51 -070014TEST(isContentTypeAllowed, PositiveTest)
Nan Zhoua02b5862022-06-21 17:46:56 +000015{
Ed Tanous99351cd2022-08-07 16:42:51 -070016 EXPECT_TRUE(isContentTypeAllowed("*/*, application/octet-stream",
17 ContentType::OctetStream));
18 EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
19 ContentType::OctetStream));
20 EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML));
21 EXPECT_TRUE(isContentTypeAllowed("application/json", ContentType::JSON));
22 EXPECT_TRUE(isContentTypeAllowed("application/cbor", ContentType::CBOR));
23 EXPECT_TRUE(
24 isContentTypeAllowed("application/json, text/html", ContentType::HTML));
Nan Zhoua02b5862022-06-21 17:46:56 +000025}
26
Ed Tanous99351cd2022-08-07 16:42:51 -070027TEST(isContentTypeAllowed, NegativeTest)
George Liu647b3cd2021-07-05 12:43:56 +080028{
Ed Tanous99351cd2022-08-07 16:42:51 -070029 EXPECT_FALSE(
30 isContentTypeAllowed("application/octet-stream", ContentType::HTML));
31 EXPECT_FALSE(isContentTypeAllowed("application/html", ContentType::JSON));
32 EXPECT_FALSE(isContentTypeAllowed("application/json", ContentType::CBOR));
33 EXPECT_FALSE(isContentTypeAllowed("application/cbor", ContentType::HTML));
34 EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
35 ContentType::OctetStream));
Nan Zhoua02b5862022-06-21 17:46:56 +000036}
37
Ed Tanous99351cd2022-08-07 16:42:51 -070038TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
Nan Zhoua02b5862022-06-21 17:46:56 +000039{
Ed Tanous99351cd2022-08-07 16:42:51 -070040 EXPECT_TRUE(
41 isContentTypeAllowed("text/html, */*", ContentType::OctetStream));
Nan Zhoua02b5862022-06-21 17:46:56 +000042}
43
Ed Tanous99351cd2022-08-07 16:42:51 -070044TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
Gunnar Millsa3526fe2022-02-02 21:56:44 +000045{
Ed Tanous99351cd2022-08-07 16:42:51 -070046 EXPECT_TRUE(
47 isContentTypeAllowed("text/html, */*;q=0.8", ContentType::OctetStream));
Gunnar Millsa3526fe2022-02-02 21:56:44 +000048}
49
Ed Tanous99351cd2022-08-07 16:42:51 -070050TEST(getPreferedContentType, PositiveTest)
Gunnar Millsa3526fe2022-02-02 21:56:44 +000051{
Ed Tanous99351cd2022-08-07 16:42:51 -070052 std::array<ContentType, 1> contentType{ContentType::HTML};
53 EXPECT_EQ(
54 getPreferedContentType("text/html, application/json", contentType),
55 ContentType::HTML);
56
57 std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
58 EXPECT_EQ(getPreferedContentType("text/html, application/json", htmlJson),
59 ContentType::HTML);
60
61 std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
62 EXPECT_EQ(getPreferedContentType("text/html, application/json", jsonHtml),
63 ContentType::HTML);
64
65 std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
66 EXPECT_EQ(
67 getPreferedContentType("application/cbor, application::json", cborJson),
68 ContentType::CBOR);
69
70 EXPECT_EQ(getPreferedContentType("application/json", cborJson),
71 ContentType::JSON);
Gunnar Millsa3526fe2022-02-02 21:56:44 +000072}
73
Ed Tanous99351cd2022-08-07 16:42:51 -070074TEST(getPreferedContentType, NegativeTest)
Nan Zhoua02b5862022-06-21 17:46:56 +000075{
Ed Tanous99351cd2022-08-07 16:42:51 -070076 std::array<ContentType, 1> contentType{ContentType::CBOR};
77 EXPECT_EQ(
78 getPreferedContentType("text/html, application/json", contentType),
79 ContentType::NoMatch);
George Liu647b3cd2021-07-05 12:43:56 +080080}
Nan Zhouad3edd42022-06-21 17:46:44 +000081} // namespace
Gunnar Millsa3526fe2022-02-02 21:56:44 +000082} // namespace http_helpers