blob: a957fdd9eaf0a43b2c2b9df2130c72b4e682ca14 [file] [log] [blame]
Harald Welte55e879c2021-02-19 13:16:28 +01001module HTTP_Adapter {
2
3/* HTTP Adapter component, originally part of Integration Tests for osmo-remsim-server
4 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
5 * All rights reserved.
6 *
7 * Released under the terms of GNU General Public License, Version 2 or
8 * (at your option) any later version.
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 *
12 * This test suite tests osmo-remsim-server by attaching to the external interfaces
13 * such as RSPRO for simulated clients + bankds and RSRES (REST backend interface).
14 */
15
16import from HTTPmsg_Types all;
17import from HTTPmsg_PortType all;
18
19type component http_CT {
20 port HTTPmsg_PT HTTP;
21 var charstring g_http_host;
22 var integer g_http_port;
23};
24
25function f_http_init(charstring host, integer http_port) runs on http_CT {
26 map(self:HTTP, system:HTTP);
27 g_http_host := host;
28 g_http_port := http_port;
29}
30
31template (value) Connect ts_HTTP_Connect(template (value) charstring hostname,
32 template (value) integer http_port := 80,
33 template (value) boolean use_ssl := false) := {
34 hostname := hostname,
35 portnumber := http_port,
36 use_ssl := use_ssl
37}
38template (value) Close ts_HTTP_Close := { client_id := omit };
39
40template (value) HeaderLines ts_HTTP_Header(charstring body) := {
41 { header_name := "Content-Type", header_value := "application/json" },
42 { header_name := "Content-Length", header_value := int2str(lengthof(body)) }
43}
44
45template (value) HTTPMessage ts_HTTP_Req(charstring url,
46 charstring method := "GET",
47 charstring body := "",
48 integer v_maj := 1, integer v_min := 1) := {
49 request := {
50 client_id := omit,
51 method := method,
52 uri := url,
53 version_major := v_maj,
54 version_minor := v_min,
55 header := ts_HTTP_Header(body),
56 body := body
57 }
58}
59
60template HTTPMessage tr_HTTP_Resp(template integer sts := ?) := {
61 response := {
62 client_id := ?,
63 version_major := ?,
64 version_minor := ?,
65 statuscode := sts,
66 statustext := ?,
67 header := ?,
68 body := ?
69 }
70};
71
72template HTTPMessage tr_HTTP_Resp2xx := tr_HTTP_Resp((200..299));
73
Harald Welte205b5372021-02-22 09:16:21 +010074function f_http_tx_request(charstring url, charstring method := "GET", charstring body := "")
75runs on http_CT {
76 HTTP.send(ts_HTTP_Connect(g_http_host, g_http_port));
77 HTTP.receive(Connect_result:?);
78 HTTP.send(ts_HTTP_Req(url, method, body));
79}
80
81function f_http_rx_response(template HTTPMessage exp := tr_HTTP_Resp2xx, float tout := 2.0)
Harald Welte55e879c2021-02-19 13:16:28 +010082runs on http_CT return HTTPMessage {
83 var HTTPMessage resp;
Harald Weltead9d8362021-02-22 10:23:00 +010084 timer T := tout;
Harald Welte55e879c2021-02-19 13:16:28 +010085 T.start;
86 alt {
87 [] HTTP.receive(exp) -> value resp {
88 setverdict(pass);
89 }
90 [] HTTP.receive(tr_HTTP_Resp) -> value resp {
91 setverdict(fail, "Unexpected HTTP response ", resp);
92 }
93 [] T.timeout {
94 setverdict(fail, "Timeout waiting for HTTP response");
95 self.stop;
96 }
97 }
98 HTTP.send(ts_HTTP_Close);
99 return resp;
100}
101
Harald Welte205b5372021-02-22 09:16:21 +0100102/* run a HTTP request and return the response */
103function f_http_transact(charstring url, charstring method := "GET",
104 charstring body := "", template HTTPMessage exp := tr_HTTP_Resp2xx,
105 float tout := 2.0)
106runs on http_CT return HTTPMessage {
107 f_http_tx_request(url, method, body);
108 return f_http_rx_response(exp, tout);
109}
110
Harald Welte55e879c2021-02-19 13:16:28 +0100111}