blob: c78c40a5d0ffc0492b481ac97f08bf319fb79181 [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;
Philipp Maier6406e6d2024-03-26 13:37:11 +010018import from Native_Functions all;
Harald Welte55e879c2021-02-19 13:16:28 +010019
20type component http_CT {
21 port HTTPmsg_PT HTTP;
Philipp Maier260f7082024-04-19 13:12:49 +020022 var HTTP_Adapter_Params g_pars;
Harald Welte55e879c2021-02-19 13:16:28 +010023};
24
Philipp Maier260f7082024-04-19 13:12:49 +020025type record HTTP_Adapter_Params {
26 charstring http_host,
Philipp Maier832b1ef2024-03-26 17:12:37 +010027 integer http_port,
28 boolean use_ssl
Philipp Maier260f7082024-04-19 13:12:49 +020029};
30
31function f_http_init(HTTP_Adapter_Params pars) runs on http_CT {
Harald Welte55e879c2021-02-19 13:16:28 +010032 map(self:HTTP, system:HTTP);
Philipp Maier260f7082024-04-19 13:12:49 +020033 g_pars := pars;
Harald Welte55e879c2021-02-19 13:16:28 +010034}
35
36template (value) Connect ts_HTTP_Connect(template (value) charstring hostname,
37 template (value) integer http_port := 80,
38 template (value) boolean use_ssl := false) := {
39 hostname := hostname,
40 portnumber := http_port,
41 use_ssl := use_ssl
42}
43template (value) Close ts_HTTP_Close := { client_id := omit };
44
Philipp Maier6406e6d2024-03-26 13:37:11 +010045/* function to add HeaderLines to a an existing set of HeaderLines. HeaderLines that are already present, are updated. */
46function f_overlay_HTTP_Header(HeaderLines hdr, HeaderLines additional_hdr) return template (value) HeaderLines
47{
48 var integer i;
49 var integer k;
50 var boolean updated;
51
52 for (i := 0; i < lengthof(additional_hdr); i := i+1) {
53 updated := false;
54 for (k := 0; k < lengthof(hdr); k := k+1) {
55 if (f_str_tolower(hdr[k].header_name) == f_str_tolower(additional_hdr[i].header_name)) {
56 hdr[k] := additional_hdr[i];
57 updated := true;
58 }
59 }
60 if (updated == false) {
61 hdr := hdr & { additional_hdr[i] };
62 }
63 }
64
65 return hdr;
66}
67
68template (value) HeaderLine ts_HeaderLine(charstring header_name, charstring header_value) := {
69 header_name := header_name,
70 header_value := header_value
71}
72
73function f_ts_HTTP_Header(template (omit) charstring body := omit,
Philipp Maier28e7ad32024-04-24 15:44:15 +020074 template (omit) octetstring binary_body := omit,
Philipp Maier6406e6d2024-03-26 13:37:11 +010075 template (omit) charstring host := omit,
76 HeaderLines custom_hdr := { })
77return template (value) HeaderLines {
78 var HeaderLines hdr := { };
79
Philipp Maier28e7ad32024-04-24 15:44:15 +020080 /* Make sure we never use body or binary_body at the same time */
81 if (not istemplatekind(body, "omit") and not istemplatekind(binary_body, "omit")) {
82 setverdict(fail, "use wither (ascii) body or binary_body");
83 }
84
Philipp Maier6406e6d2024-03-26 13:37:11 +010085 /* Build default header */
86 if (not istemplatekind(host, "omit")) {
87 hdr := hdr & {valueof(ts_HeaderLine("Host", valueof(host)))};
88 }
89 hdr := hdr & {{ header_name := "Content-Type", header_value := "application/json" }};
90 if (not istemplatekind(body, "omit")) {
91 hdr := hdr & {valueof(ts_HeaderLine("Content-Length", int2str(lengthof(body))))};
92 }
Philipp Maier28e7ad32024-04-24 15:44:15 +020093 else if (not istemplatekind(binary_body, "omit")) {
94 hdr := hdr & {valueof(ts_HeaderLine("Content-Length", int2str(lengthof(binary_body))))};
95 }
Philipp Maier6406e6d2024-03-26 13:37:11 +010096
97 return f_overlay_HTTP_Header(hdr, custom_hdr);
Harald Welte55e879c2021-02-19 13:16:28 +010098}
99
Philipp Maieref20fa02024-03-26 15:50:22 +0100100function f_ts_body_or_empty(template (omit) charstring body) return template (value) charstring {
101 if (istemplatekind(body, "omit")) {
102 return "";
103 }
104 return body;
105}
106
Harald Welte55e879c2021-02-19 13:16:28 +0100107template (value) HTTPMessage ts_HTTP_Req(charstring url,
108 charstring method := "GET",
Philipp Maieref20fa02024-03-26 15:50:22 +0100109 template (omit) charstring body := omit,
Philipp Maier4a19b472024-03-25 15:08:43 +0100110 integer v_maj := 1, integer v_min := 1,
Philipp Maier6406e6d2024-03-26 13:37:11 +0100111 charstring host,
112 HeaderLines custom_hdr := { }) := {
Harald Welte55e879c2021-02-19 13:16:28 +0100113 request := {
114 client_id := omit,
115 method := method,
116 uri := url,
117 version_major := v_maj,
118 version_minor := v_min,
Philipp Maier28e7ad32024-04-24 15:44:15 +0200119 header := f_ts_HTTP_Header(body, omit, host, custom_hdr),
Philipp Maieref20fa02024-03-26 15:50:22 +0100120 body := f_ts_body_or_empty(body)
Harald Welte55e879c2021-02-19 13:16:28 +0100121 }
122}
123
Philipp Maier28e7ad32024-04-24 15:44:15 +0200124function f_ts_body_or_empty_bin(template (omit) octetstring body) return template (value) octetstring {
125 if (istemplatekind(body, "omit")) {
126 return ''O;
127 }
128 return body;
129}
130
131template (value) HTTPMessage ts_HTTP_Req_Bin(charstring url,
132 charstring method := "GET",
133 template (omit) octetstring body := omit,
134 integer v_maj := 1, integer v_min := 1,
135 charstring host,
136 HeaderLines custom_hdr := { }) := {
137 request_binary := {
138 client_id := omit,
139 method := method,
140 uri := url,
141 version_major := v_maj,
142 version_minor := v_min,
143 header := f_ts_HTTP_Header(omit, body, host, custom_hdr),
144 body := f_ts_body_or_empty_bin(body)
145 }
146}
147
148
Harald Welte55e879c2021-02-19 13:16:28 +0100149template HTTPMessage tr_HTTP_Resp(template integer sts := ?) := {
150 response := {
151 client_id := ?,
152 version_major := ?,
153 version_minor := ?,
154 statuscode := sts,
155 statustext := ?,
156 header := ?,
157 body := ?
158 }
159};
160
Philipp Maier28e7ad32024-04-24 15:44:15 +0200161template HTTPMessage tr_HTTP_Resp_Bin(template integer sts := ?) := {
162 response_binary := {
163 client_id := ?,
164 version_major := ?,
165 version_minor := ?,
166 statuscode := sts,
167 statustext := ?,
168 header := ?,
169 body := ?
170 }
171};
172
Harald Welte55e879c2021-02-19 13:16:28 +0100173template HTTPMessage tr_HTTP_Resp2xx := tr_HTTP_Resp((200..299));
174
Philipp Maier28e7ad32024-04-24 15:44:15 +0200175function f_http_tx_request(charstring url, charstring method := "GET",
176 template charstring body := omit,
177 template octetstring binary_body := omit,
Philipp Maier6406e6d2024-03-26 13:37:11 +0100178 HeaderLines custom_hdr := { })
Harald Welte205b5372021-02-22 09:16:21 +0100179runs on http_CT {
Philipp Maier832b1ef2024-03-26 17:12:37 +0100180 HTTP.send(ts_HTTP_Connect(g_pars.http_host, g_pars.http_port, g_pars.use_ssl));
Harald Welte205b5372021-02-22 09:16:21 +0100181 HTTP.receive(Connect_result:?);
Philipp Maier28e7ad32024-04-24 15:44:15 +0200182
183 if (not istemplatekind(body, "omit")) {
184 HTTP.send(ts_HTTP_Req(url, method, body, host := g_pars.http_host & ":" & int2str(g_pars.http_port), custom_hdr := custom_hdr));
185 } else if (not istemplatekind(binary_body, "omit")) {
186 HTTP.send(ts_HTTP_Req_Bin(url, method, binary_body, host := g_pars.http_host & ":" & int2str(g_pars.http_port), custom_hdr := custom_hdr));
187 }
Harald Welte205b5372021-02-22 09:16:21 +0100188}
189
190function f_http_rx_response(template HTTPMessage exp := tr_HTTP_Resp2xx, float tout := 2.0)
Harald Welte55e879c2021-02-19 13:16:28 +0100191runs on http_CT return HTTPMessage {
192 var HTTPMessage resp;
Harald Weltead9d8362021-02-22 10:23:00 +0100193 timer T := tout;
Harald Welte55e879c2021-02-19 13:16:28 +0100194 T.start;
195 alt {
196 [] HTTP.receive(exp) -> value resp {
197 setverdict(pass);
198 }
199 [] HTTP.receive(tr_HTTP_Resp) -> value resp {
200 setverdict(fail, "Unexpected HTTP response ", resp);
201 }
Philipp Maier28e7ad32024-04-24 15:44:15 +0200202 [] HTTP.receive(tr_HTTP_Resp_Bin) -> value resp {
203 setverdict(fail, "Unexpected (binary) HTTP response ", resp);
204 }
Harald Welte55e879c2021-02-19 13:16:28 +0100205 [] T.timeout {
206 setverdict(fail, "Timeout waiting for HTTP response");
207 self.stop;
208 }
209 }
210 HTTP.send(ts_HTTP_Close);
211 return resp;
212}
213
Harald Welte205b5372021-02-22 09:16:21 +0100214/* run a HTTP request and return the response */
215function f_http_transact(charstring url, charstring method := "GET",
Philipp Maier28e7ad32024-04-24 15:44:15 +0200216 template (omit) charstring body := omit,
217 template (omit) octetstring binary_body := omit,
218 template HTTPMessage exp := tr_HTTP_Resp2xx,
Philipp Maier6406e6d2024-03-26 13:37:11 +0100219 float tout := 2.0, HeaderLines custom_hdr := { })
Harald Welte205b5372021-02-22 09:16:21 +0100220runs on http_CT return HTTPMessage {
Philipp Maier28e7ad32024-04-24 15:44:15 +0200221 f_http_tx_request(url, method, body, binary_body, custom_hdr);
Harald Welte205b5372021-02-22 09:16:21 +0100222 return f_http_rx_response(exp, tout);
223}
224
Harald Welte55e879c2021-02-19 13:16:28 +0100225}