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/net/IPAddress.h"
18 #include "jaegertracing/testutils/TracerUtil.h"
19 #include <gtest/gtest.h>
22 #include <opentracing/string_view.h>
23 #include <opentracing/tracer.h>
28 #include "jaegertracing/net/Socket.h"
29 #include "jaegertracing/net/http/Request.h"
30 #include "jaegertracing/net/http/Response.h"
34 namespace jaegertracing
{
38 TEST(HTTPTransporter
, testSpanReporting
)
40 net::IPAddress serverAddr
;
41 std::promise
<void> started
;
44 net::http::Method method
;
45 std::string contentType
;
46 std::string acceptType
;
48 std::thread
serverThread(
49 [&serverAddr
, &started
, &target
, &method
, &contentType
, &acceptType
]() {
51 socket
.open(AF_INET
, SOCK_STREAM
);
52 socket
.bind(net::IPAddress::v4("127.0.0.1", 0));
53 ::sockaddr_storage addrStorage
;
54 ::socklen_t addrLen
= sizeof(addrStorage
);
55 const auto returnCode
=
56 ::getsockname(socket
.handle(),
57 reinterpret_cast<::sockaddr
*>(&addrStorage
),
59 ASSERT_EQ(0, returnCode
);
60 serverAddr
= net::IPAddress(addrStorage
, addrLen
);
62 // Listening to the port
65 // Unblocking the client
68 // Waiting for the client to connect
69 auto clientSocket
= socket
.accept();
71 net::http::Request request
= net::http::Request::read(clientSocket
);
73 target
= request
.target();
74 method
= request
.method();
76 for (auto header
: request
.headers()) {
77 if (header
.key() == "Content-Type") {
78 contentType
= header
.value();
81 if (header
.key() == "Accept") {
82 acceptType
= header
.value();
87 "HTTP/1.1 202 Accepted\r\nDate: Tue, 10 Sep 2019 09:03:26 "
88 "GMT\r\nContent-Length: 0\r\n\r\n");
90 ::send(clientSocket
.handle(), answer
.c_str(), answer
.size(), 0);
93 started
.get_future().wait();
95 std::ostringstream oss
;
96 oss
<< "http://127.0.0.1:" << serverAddr
.port() << "/api/traces";
97 std::shared_ptr
<opentracing::Tracer
> tracer
=
98 ::jaegertracing::testutils::TracerUtil::buildTracer(oss
.str());
101 auto span1
= tracer
->StartSpan("tracedFunction");
106 ASSERT_EQ(std::string("/api/traces?format=jaeger.thrift"), target
);
107 ASSERT_EQ(net::http::Method::POST
, method
);
108 ASSERT_EQ(std::string("application/x-thrift"), contentType
);
109 ASSERT_EQ(std::string("application/x-thrift"), acceptType
);
113 } // namespace jaegertracing