|
3 | 3 | * SPDX-License-Identifier: Apache-2.0. |
4 | 4 | */ |
5 | 5 |
|
6 | | -#include <aws/testing/AwsCppSdkGTestSuite.h> |
7 | 6 | #include <aws/core/http/URI.h> |
8 | 7 | #include <aws/core/http/standard/StandardHttpRequest.h> |
9 | 8 | #include <aws/crt/http/HttpRequestResponse.h> |
| 9 | +#include <aws/testing/AwsCppSdkGTestSuite.h> |
10 | 10 |
|
11 | 11 | using namespace Aws::Http; |
12 | 12 | using namespace Aws::Http::Standard; |
13 | 13 |
|
14 | | -namespace |
15 | | -{ |
16 | | - // Resets the process-global path-separator flag when the test leaves, including on assertion failure. |
17 | | - class PreservePathSeparatorsGuard |
18 | | - { |
19 | | - public: |
20 | | - explicit PreservePathSeparatorsGuard(bool preserve) |
21 | | - { |
22 | | - SetPreservePathSeparators(preserve); |
23 | | - } |
24 | | - |
25 | | - ~PreservePathSeparatorsGuard() |
26 | | - { |
27 | | - SetPreservePathSeparators(false); |
28 | | - } |
29 | | - |
30 | | - PreservePathSeparatorsGuard(const PreservePathSeparatorsGuard&) = delete; |
31 | | - PreservePathSeparatorsGuard& operator=(const PreservePathSeparatorsGuard&) = delete; |
32 | | - }; |
33 | | - |
34 | | - Aws::String CrtRequestPath(const char* uri, HttpMethod method) |
35 | | - { |
36 | | - StandardHttpRequest request(URI(uri), method); |
37 | | - auto crtRequest = request.ToCrtHttpRequest(); |
38 | | - if (!crtRequest) |
39 | | - { |
40 | | - ADD_FAILURE() << "ToCrtHttpRequest returned null for " << uri; |
41 | | - return {}; |
42 | | - } |
43 | | - |
44 | | - auto path = crtRequest->GetPath(); |
45 | | - if (!path.has_value()) |
46 | | - { |
47 | | - ADD_FAILURE() << "CRT request has no path for " << uri; |
48 | | - return {}; |
49 | | - } |
50 | | - |
51 | | - return Aws::String(reinterpret_cast<const char*>(path->ptr), path->len); |
52 | | - } |
53 | | -} |
| 14 | +namespace { |
| 15 | +// Resets the process-global path-separator flag when the test leaves, including on assertion failure. |
| 16 | +class PreservePathSeparatorsGuard { |
| 17 | + public: |
| 18 | + explicit PreservePathSeparatorsGuard(bool preserve) { SetPreservePathSeparators(preserve); } |
54 | 19 |
|
55 | | -class HttpRequestConversionTest : public Aws::Testing::AwsCppSdkGTestSuite |
56 | | -{ |
| 20 | + ~PreservePathSeparatorsGuard() { SetPreservePathSeparators(false); } |
| 21 | + |
| 22 | + PreservePathSeparatorsGuard(const PreservePathSeparatorsGuard&) = delete; |
| 23 | + PreservePathSeparatorsGuard& operator=(const PreservePathSeparatorsGuard&) = delete; |
57 | 24 | }; |
58 | 25 |
|
59 | | -TEST_F(HttpRequestConversionTest, OriginFormNonDefaultPort) |
60 | | -{ |
61 | | - EXPECT_EQ("/bucket/key?uploadId=abc&partNumber=1", |
62 | | - CrtRequestPath("http://example.com:9000/bucket/key?uploadId=abc&partNumber=1", HttpMethod::HTTP_PUT)); |
| 26 | +Aws::String CrtRequestPath(const char* uri, HttpMethod method) { |
| 27 | + StandardHttpRequest request(URI(uri), method); |
| 28 | + auto crtRequest = request.ToCrtHttpRequest(); |
| 29 | + if (!crtRequest) { |
| 30 | + ADD_FAILURE() << "ToCrtHttpRequest returned null for " << uri; |
| 31 | + return {}; |
| 32 | + } |
| 33 | + |
| 34 | + auto path = crtRequest->GetPath(); |
| 35 | + if (!path.has_value()) { |
| 36 | + ADD_FAILURE() << "CRT request has no path for " << uri; |
| 37 | + return {}; |
| 38 | + } |
| 39 | + |
| 40 | + return Aws::String(reinterpret_cast<const char*>(path->ptr), path->len); |
63 | 41 | } |
| 42 | +} // namespace |
| 43 | + |
| 44 | +class HttpRequestConversionTest : public Aws::Testing::AwsCppSdkGTestSuite {}; |
64 | 45 |
|
65 | | -TEST_F(HttpRequestConversionTest, OriginFormHttpsPath) |
66 | | -{ |
67 | | - EXPECT_EQ("/my-bucket/my-key", |
68 | | - CrtRequestPath("https://s3.amazonaws.com/my-bucket/my-key", HttpMethod::HTTP_GET)); |
| 46 | +TEST_F(HttpRequestConversionTest, OriginFormNonDefaultPort) { |
| 47 | + EXPECT_EQ("/bucket/key?uploadId=abc&partNumber=1", |
| 48 | + CrtRequestPath("http://example.com:9000/bucket/key?uploadId=abc&partNumber=1", HttpMethod::HTTP_PUT)); |
69 | 49 | } |
70 | 50 |
|
71 | | -TEST_F(HttpRequestConversionTest, OriginFormQueryString) |
72 | | -{ |
73 | | - EXPECT_EQ("/path/to/file?query=value&foo=bar", |
74 | | - CrtRequestPath("https://storage.example.com:443/path/to/file?query=value&foo=bar", HttpMethod::HTTP_POST)); |
| 51 | +TEST_F(HttpRequestConversionTest, OriginFormHttpsPath) { |
| 52 | + EXPECT_EQ("/my-bucket/my-key", CrtRequestPath("https://s3.amazonaws.com/my-bucket/my-key", HttpMethod::HTTP_GET)); |
75 | 53 | } |
76 | 54 |
|
77 | | -TEST_F(HttpRequestConversionTest, OriginFormRootPath) |
78 | | -{ |
79 | | - EXPECT_EQ("/", CrtRequestPath("http://localhost:8080/", HttpMethod::HTTP_HEAD)); |
| 55 | +TEST_F(HttpRequestConversionTest, OriginFormQueryString) { |
| 56 | + EXPECT_EQ("/path/to/file?query=value&foo=bar", |
| 57 | + CrtRequestPath("https://storage.example.com:443/path/to/file?query=value&foo=bar", HttpMethod::HTTP_POST)); |
80 | 58 | } |
81 | 59 |
|
| 60 | +TEST_F(HttpRequestConversionTest, OriginFormRootPath) { EXPECT_EQ("/", CrtRequestPath("http://localhost:8080/", HttpMethod::HTTP_HEAD)); } |
| 61 | + |
82 | 62 | // Old absolute-form code dropped a root path and emitted "https://host?list-type=2". |
83 | | -TEST_F(HttpRequestConversionTest, OriginFormRootPathWithQuery) |
84 | | -{ |
85 | | - EXPECT_EQ("/?list-type=2", CrtRequestPath("https://host/?list-type=2", HttpMethod::HTTP_GET)); |
| 63 | +TEST_F(HttpRequestConversionTest, OriginFormRootPathWithQuery) { |
| 64 | + EXPECT_EQ("/?list-type=2", CrtRequestPath("https://host/?list-type=2", HttpMethod::HTTP_GET)); |
86 | 65 | } |
87 | 66 |
|
88 | | -TEST_F(HttpRequestConversionTest, OriginFormTrailingSlash) |
89 | | -{ |
90 | | - EXPECT_EQ("/bucket/prefix/", CrtRequestPath("https://host/bucket/prefix/", HttpMethod::HTTP_GET)); |
| 67 | +TEST_F(HttpRequestConversionTest, OriginFormTrailingSlash) { |
| 68 | + EXPECT_EQ("/bucket/prefix/", CrtRequestPath("https://host/bucket/prefix/", HttpMethod::HTTP_GET)); |
91 | 69 | } |
92 | 70 |
|
93 | | -TEST_F(HttpRequestConversionTest, OriginFormEncodesReservedCharacters) |
94 | | -{ |
95 | | - EXPECT_EQ("/bucket/a%3Ab%20c%2Bd", CrtRequestPath("https://host/bucket/a:b c+d", HttpMethod::HTTP_PUT)); |
| 71 | +TEST_F(HttpRequestConversionTest, OriginFormEncodesReservedCharacters) { |
| 72 | + EXPECT_EQ("/bucket/a%3Ab%20c%2Bd", CrtRequestPath("https://host/bucket/a:b c+d", HttpMethod::HTTP_PUT)); |
96 | 73 | } |
97 | 74 |
|
98 | | -TEST_F(HttpRequestConversionTest, OriginFormPreservesLeadingSlashInKey) |
99 | | -{ |
100 | | - PreservePathSeparatorsGuard guard(true); |
101 | | - (void)guard; |
102 | | - EXPECT_EQ("//key", CrtRequestPath("https://host//key", HttpMethod::HTTP_GET)); |
| 75 | +TEST_F(HttpRequestConversionTest, OriginFormPreservesLeadingSlashInKey) { |
| 76 | + PreservePathSeparatorsGuard guard(true); |
| 77 | + (void)guard; |
| 78 | + EXPECT_EQ("//key", CrtRequestPath("https://host//key", HttpMethod::HTTP_GET)); |
103 | 79 | } |
0 commit comments