forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPClientTests.cpp
94 lines (76 loc) · 3.63 KB
/
HTTPClientTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//===-- llvm/unittest/Debuginfod/HTTPClientTests.cpp - unit tests ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Debuginfod/HTTPClient.h"
#include "llvm/Support/Errc.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
using namespace llvm;
TEST(BufferedHTTPResponseHandler, Lifecycle) {
BufferedHTTPResponseHandler Handler;
EXPECT_THAT_ERROR(Handler.handleHeaderLine("Content-Length: 36\r\n"),
Succeeded());
EXPECT_THAT_ERROR(Handler.handleBodyChunk("body:"), Succeeded());
EXPECT_THAT_ERROR(Handler.handleBodyChunk("this puts the total at 36 chars"),
Succeeded());
EXPECT_EQ(Handler.ResponseBuffer.Body->MemoryBuffer::getBuffer(),
"body:this puts the total at 36 chars");
// Additional content should be rejected by the handler.
EXPECT_THAT_ERROR(
Handler.handleBodyChunk("extra content past the content-length"),
Failed<llvm::StringError>());
// Test response code is set.
EXPECT_THAT_ERROR(Handler.handleStatusCode(200u), Succeeded());
EXPECT_EQ(Handler.ResponseBuffer.Code, 200u);
EXPECT_THAT_ERROR(Handler.handleStatusCode(400u), Succeeded());
EXPECT_EQ(Handler.ResponseBuffer.Code, 400u);
}
TEST(BufferedHTTPResponseHandler, NoContentLengthLifecycle) {
BufferedHTTPResponseHandler Handler;
EXPECT_EQ(Handler.ResponseBuffer.Code, 0u);
EXPECT_EQ(Handler.ResponseBuffer.Body, nullptr);
// A body chunk passed before the content-length header is an error.
EXPECT_THAT_ERROR(Handler.handleBodyChunk("body"),
Failed<llvm::StringError>());
EXPECT_THAT_ERROR(Handler.handleHeaderLine("a header line"), Succeeded());
EXPECT_THAT_ERROR(Handler.handleBodyChunk("body"),
Failed<llvm::StringError>());
}
TEST(BufferedHTTPResponseHandler, ZeroContentLength) {
BufferedHTTPResponseHandler Handler;
EXPECT_THAT_ERROR(Handler.handleHeaderLine("Content-Length: 0"), Succeeded());
EXPECT_NE(Handler.ResponseBuffer.Body, nullptr);
EXPECT_EQ(Handler.ResponseBuffer.Body->getBufferSize(), 0u);
// All content should be rejected by the handler.
EXPECT_THAT_ERROR(Handler.handleBodyChunk("non-empty body content"),
Failed<llvm::StringError>());
}
TEST(BufferedHTTPResponseHandler, MalformedContentLength) {
// Check that several invalid content lengths are ignored.
BufferedHTTPResponseHandler Handler;
EXPECT_EQ(Handler.ResponseBuffer.Body, nullptr);
EXPECT_THAT_ERROR(Handler.handleHeaderLine("Content-Length: fff"),
Succeeded());
EXPECT_EQ(Handler.ResponseBuffer.Body, nullptr);
EXPECT_THAT_ERROR(Handler.handleHeaderLine("Content-Length: "),
Succeeded());
EXPECT_EQ(Handler.ResponseBuffer.Body, nullptr);
using namespace std::string_literals;
EXPECT_THAT_ERROR(Handler.handleHeaderLine("Content-Length: \0\0\0"s),
Succeeded());
EXPECT_EQ(Handler.ResponseBuffer.Body, nullptr);
EXPECT_THAT_ERROR(Handler.handleHeaderLine("Content-Length: -11"),
Succeeded());
EXPECT_EQ(Handler.ResponseBuffer.Body, nullptr);
// All content should be rejected by the handler because no valid
// Content-Length header has been received.
EXPECT_THAT_ERROR(Handler.handleBodyChunk("non-empty body content"),
Failed<llvm::StringError>());
}
#ifdef LLVM_ENABLE_CURL
TEST(HTTPClient, isAvailable) { EXPECT_TRUE(HTTPClient::isAvailable()); }
#endif