blob: 0885f05865e34e22c8ed34d298d1dc6058f77edb [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
Philipp Maier4a19b472024-03-25 15:08:43 +010040template (value) HeaderLines ts_HTTP_Header(charstring body, charstring host) := {
41 { header_name := "Host", header_value := host },
Harald Welte55e879c2021-02-19 13:16:28 +010042 { header_name := "Content-Type", header_value := "application/json" },
43 { header_name := "Content-Length", header_value := int2str(lengthof(body)) }
44}
45
46template (value) HTTPMessage ts_HTTP_Req(charstring url,
47 charstring method := "GET",
48 charstring body := "",
Philipp Maier4a19b472024-03-25 15:08:43 +010049 integer v_maj := 1, integer v_min := 1,
50 charstring host) := {
Harald Welte55e879c2021-02-19 13:16:28 +010051 request := {
52 client_id := omit,
53 method := method,
54 uri := url,
55 version_major := v_maj,
56 version_minor := v_min,
Philipp Maier4a19b472024-03-25 15:08:43 +010057 header := valueof(ts_HTTP_Header(body, host)),
Harald Welte55e879c2021-02-19 13:16:28 +010058 body := body
59 }
60}
61
62template HTTPMessage tr_HTTP_Resp(template integer sts := ?) := {
63 response := {
64 client_id := ?,
65 version_major := ?,
66 version_minor := ?,
67 statuscode := sts,
68 statustext := ?,
69 header := ?,
70 body := ?
71 }
72};
73
74template HTTPMessage tr_HTTP_Resp2xx := tr_HTTP_Resp((200..299));
75
Harald Welte205b5372021-02-22 09:16:21 +010076function f_http_tx_request(charstring url, charstring method := "GET", charstring body := "")
77runs on http_CT {
78 HTTP.send(ts_HTTP_Connect(g_http_host, g_http_port));
79 HTTP.receive(Connect_result:?);
Philipp Maier4a19b472024-03-25 15:08:43 +010080 HTTP.send(ts_HTTP_Req(url, method, body, host := g_http_host & ":" & int2str(g_http_port)));
Harald Welte205b5372021-02-22 09:16:21 +010081}
82
83function f_http_rx_response(template HTTPMessage exp := tr_HTTP_Resp2xx, float tout := 2.0)
Harald Welte55e879c2021-02-19 13:16:28 +010084runs on http_CT return HTTPMessage {
85 var HTTPMessage resp;
Harald Weltead9d8362021-02-22 10:23:00 +010086 timer T := tout;
Harald Welte55e879c2021-02-19 13:16:28 +010087 T.start;
88 alt {
89 [] HTTP.receive(exp) -> value resp {
90 setverdict(pass);
91 }
92 [] HTTP.receive(tr_HTTP_Resp) -> value resp {
93 setverdict(fail, "Unexpected HTTP response ", resp);
94 }
95 [] T.timeout {
96 setverdict(fail, "Timeout waiting for HTTP response");
97 self.stop;
98 }
99 }
100 HTTP.send(ts_HTTP_Close);
101 return resp;
102}
103
Harald Welte205b5372021-02-22 09:16:21 +0100104/* run a HTTP request and return the response */
105function f_http_transact(charstring url, charstring method := "GET",
106 charstring body := "", template HTTPMessage exp := tr_HTTP_Resp2xx,
107 float tout := 2.0)
108runs on http_CT return HTTPMessage {
109 f_http_tx_request(url, method, body);
110 return f_http_rx_response(exp, tout);
111}
112
Harald Welte55e879c2021-02-19 13:16:28 +0100113}