]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/jaeger-client-cpp/src/jaegertracing/utils/HTTPTransporterTest.cpp
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / jaeger-client-cpp / src / jaegertracing / utils / HTTPTransporterTest.cpp
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/net/IPAddress.h"
18 #include "jaegertracing/testutils/TracerUtil.h"
19 #include <gtest/gtest.h>
20 #include <iterator>
21 #include <memory>
22 #include <opentracing/string_view.h>
23 #include <opentracing/tracer.h>
24 #include <stdexcept>
25 #include <string>
26 #include <utility>
27
28 #include "jaegertracing/net/Socket.h"
29 #include "jaegertracing/net/http/Request.h"
30 #include "jaegertracing/net/http/Response.h"
31 #include <future>
32 #include <thread>
33
34 namespace jaegertracing {
35
36 namespace utils {
37
38 TEST(HTTPTransporter, testSpanReporting)
39 {
40 net::IPAddress serverAddr;
41 std::promise<void> started;
42
43 std::string target;
44 net::http::Method method;
45 std::string contentType;
46 std::string acceptType;
47
48 std::thread serverThread(
49 [&serverAddr, &started, &target, &method, &contentType, &acceptType]() {
50 net::Socket socket;
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),
58 &addrLen);
59 ASSERT_EQ(0, returnCode);
60 serverAddr = net::IPAddress(addrStorage, addrLen);
61
62 // Listening to the port
63 socket.listen();
64
65 // Unblocking the client
66 started.set_value();
67
68 // Waiting for the client to connect
69 auto clientSocket = socket.accept();
70
71 net::http::Request request = net::http::Request::read(clientSocket);
72
73 target = request.target();
74 method = request.method();
75
76 for (auto header : request.headers()) {
77 if (header.key() == "Content-Type") {
78 contentType = header.value();
79 }
80
81 if (header.key() == "Accept") {
82 acceptType = header.value();
83 }
84 }
85
86 std::string answer(
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");
89
90 ::send(clientSocket.handle(), answer.c_str(), answer.size(), 0);
91 });
92
93 started.get_future().wait();
94
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());
99
100 {
101 auto span1 = tracer->StartSpan("tracedFunction");
102 }
103
104 serverThread.join();
105
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);
110 }
111
112 } // namespace utils
113 } // namespace jaegertracing