File: httpbin.h.in

package info (click to toggle)
net-cpp 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 676 kB
  • sloc: cpp: 3,649; ansic: 185; makefile: 24
file content (126 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 *              Gary Wang  <gary.wang@canonical.com>
 */

#ifndef HTTPBIN_H_
#define HTTPBIN_H_

#include <core/posix/exec.h>

#include <thread>

/**
 *
 * Testing an HTTP Library can become difficult sometimes. Postbin is fantastic
 * for testing POST requests, but not much else. This exists to cover all kinds
 * of HTTP scenarios. Additional endpoints are being considered (e.g.
 * /deflate).
 *
 * All endpoint responses are JSON-encoded.
 *
 */
namespace httpbin
{
/** Fires up a local instance of httpbin. */
struct Instance
{
    Instance()
        : server
          {
              core::posix::exec("/usr/bin/python3", {"-c", "from httpbin import app; app.run()"}, {}, core::posix::StandardStream::stdout /*| core::posix::StandardStream::stderr*/)
          }
    {
        std::this_thread::sleep_for(std::chrono::milliseconds{1000});
    }

    ~Instance()
    {
        try
        {
            server.send_signal_or_throw(core::posix::Signal::sig_kill);
            server.wait_for(core::posix::wait::Flags::untraced);
        } catch(...)
        {
            // Just ignoring errors here.
        }
    }

    core::posix::ChildProcess server;
};

constexpr const char* host
{
    "http://127.0.0.1:5000"
};

namespace resources
{
/** A non-existing resource */
const char* does_not_exist()
{
    return "/does_not_exist";
}
/** Returns Origin IP. */
const char* ip()
{
    return "/ip";
}
/** Returns user-agent. */
const char* user_agent()
{
    return "/user-agent";
}
/** Returns header dict. */
const char* headers()
{
    return "/headers";
}
/** Returns GET data. */
const char* get()
{
    return "/get";
}
/** Returns POST data. */
const char* post()
{
    return "/post";
}
/** Returns PUT data. */
const char* put()
{
    return "/put";
}
/** Returns DELETE data. */
const char* del()
{
    return "/delete";
}
/** Challenges basic authentication. */
const char* basic_auth()
{
    return "/basic-auth/user/passwd";
}
/** Challenges digest authentication. */
const char* digest_auth()
{
    return "/digest-auth/auth/user/passwd";
}
}
}

#endif // HTTPBIN_H_