2 * Copyright (c) 2017 Uber Technologies, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
24 #include <gtest/gtest.h>
30 #pragma warning(disable : 4267)
31 #pragma warning(disable : 4244)
34 namespace jaegertracing
{
41 #define READ_ERROR SOCKET_ERROR
46 static size_t read(int socketHandle
, char* buffer
, size_t size
)
48 int returnValue
= ::recv(socketHandle
, buffer
, size
, 0);
49 return (returnValue
== READ_ERROR
) ? 0 : returnValue
;
55 IPAddress serverAddress
;
56 std::promise
<void> started
;
58 std::thread
clientThread([&serverAddress
, &started
]() {
59 started
.get_future().wait();
60 get(URI::parse("http://" + serverAddress
.authority()));
64 socket
.open(AF_INET
, SOCK_STREAM
);
65 socket
.bind(IPAddress::v4("127.0.0.1", 0));
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
);
77 auto clientSocket
= socket
.accept();
78 std::array
<char, 256> buffer
;
80 const auto numRead
= read(clientSocket
.handle(), &buffer
[0], buffer
.size());
82 std::ostringstream oss
;
83 oss
<< "GET / HTTP/1.1\r\n"
85 << serverAddress
.port() << "\r\n"
86 << "User-Agent: jaegertracing/" << kJaegerClientVersion
<< "\r\n\r\n";
87 ASSERT_EQ(oss
.str(), std::string(&buffer
[0], numRead
));
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
);
103 } // namespace jaegertracing