]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include "jaegertracing/Constants.h" | |
18 | #include "jaegertracing/net/IPAddress.h" | |
19 | #include "jaegertracing/net/Socket.h" | |
20 | #include "jaegertracing/net/URI.h" | |
21 | #include "jaegertracing/net/http/Response.h" | |
22 | #include <array> | |
23 | #include <future> | |
24 | #include <gtest/gtest.h> | |
25 | #include <ostream> | |
26 | #include <string> | |
27 | #include <thread> | |
28 | ||
29 | #ifdef _MSC_VER | |
30 | #pragma warning(disable : 4267) | |
31 | #pragma warning(disable : 4244) | |
32 | #endif | |
33 | ||
34 | namespace jaegertracing { | |
35 | namespace net { | |
36 | namespace http { | |
37 | ||
38 | namespace { | |
39 | ||
40 | #ifdef WIN32 | |
41 | #define READ_ERROR SOCKET_ERROR | |
42 | #else | |
43 | #define READ_ERROR -1 | |
44 | #endif | |
45 | ||
46 | static size_t read(int socketHandle, char* buffer, size_t size) | |
47 | { | |
48 | int returnValue = ::recv(socketHandle, buffer, size, 0); | |
49 | return (returnValue == READ_ERROR) ? 0 : returnValue; | |
50 | } | |
51 | } | |
52 | ||
53 | TEST(Response, get) | |
54 | { | |
55 | IPAddress serverAddress; | |
56 | std::promise<void> started; | |
57 | ||
58 | std::thread clientThread([&serverAddress, &started]() { | |
59 | started.get_future().wait(); | |
60 | get(URI::parse("http://" + serverAddress.authority())); | |
61 | }); | |
62 | ||
63 | Socket socket; | |
64 | socket.open(AF_INET, SOCK_STREAM); | |
65 | socket.bind(IPAddress::v4("127.0.0.1", 0)); | |
66 | socket.listen(); | |
67 | ||
68 | ::sockaddr_storage addrStorage; | |
69 | ::socklen_t addrLen = sizeof(addrStorage); | |
70 | const auto returnCode = ::getsockname( | |
71 | socket.handle(), reinterpret_cast<::sockaddr*>(&addrStorage), &addrLen); | |
72 | ASSERT_EQ(0, returnCode); | |
73 | serverAddress = IPAddress(addrStorage, addrLen); | |
74 | ||
75 | started.set_value(); | |
76 | ||
77 | auto clientSocket = socket.accept(); | |
78 | std::array<char, 256> buffer; | |
79 | ||
80 | const auto numRead = read(clientSocket.handle(), &buffer[0], buffer.size()); | |
81 | ||
82 | std::ostringstream oss; | |
83 | oss << "GET / HTTP/1.1\r\n" | |
84 | "Host: 127.0.0.1:" | |
85 | << serverAddress.port() << "\r\n" | |
86 | << "User-Agent: jaegertracing/" << kJaegerClientVersion << "\r\n\r\n"; | |
87 | ASSERT_EQ(oss.str(), std::string(&buffer[0], numRead)); | |
88 | ||
89 | oss.str(""); | |
90 | oss.clear(); | |
91 | ||
92 | std::string response("HTTP/1.1 200 OK\r\n\r\n"); | |
93 | response.append(buffer.size() * 2, '0'); | |
94 | const auto numWritten = | |
95 | ::send(clientSocket.handle(), response.c_str(), response.size(), 0); | |
96 | ASSERT_EQ(response.size(), numWritten); | |
97 | ||
98 | clientThread.join(); | |
99 | } | |
100 | ||
101 | } // namespace http | |
102 | } // namespace net | |
103 | } // namespace jaegertracing |