Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 1 | module 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 | |
| 16 | import from HTTPmsg_Types all; |
| 17 | import from HTTPmsg_PortType all; |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 18 | import from Native_Functions all; |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 19 | |
| 20 | type component http_CT { |
| 21 | port HTTPmsg_PT HTTP; |
| 22 | var charstring g_http_host; |
| 23 | var integer g_http_port; |
| 24 | }; |
| 25 | |
| 26 | function f_http_init(charstring host, integer http_port) runs on http_CT { |
| 27 | map(self:HTTP, system:HTTP); |
| 28 | g_http_host := host; |
| 29 | g_http_port := http_port; |
| 30 | } |
| 31 | |
| 32 | template (value) Connect ts_HTTP_Connect(template (value) charstring hostname, |
| 33 | template (value) integer http_port := 80, |
| 34 | template (value) boolean use_ssl := false) := { |
| 35 | hostname := hostname, |
| 36 | portnumber := http_port, |
| 37 | use_ssl := use_ssl |
| 38 | } |
| 39 | template (value) Close ts_HTTP_Close := { client_id := omit }; |
| 40 | |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 41 | /* function to add HeaderLines to a an existing set of HeaderLines. HeaderLines that are already present, are updated. */ |
| 42 | function f_overlay_HTTP_Header(HeaderLines hdr, HeaderLines additional_hdr) return template (value) HeaderLines |
| 43 | { |
| 44 | var integer i; |
| 45 | var integer k; |
| 46 | var boolean updated; |
| 47 | |
| 48 | for (i := 0; i < lengthof(additional_hdr); i := i+1) { |
| 49 | updated := false; |
| 50 | for (k := 0; k < lengthof(hdr); k := k+1) { |
| 51 | if (f_str_tolower(hdr[k].header_name) == f_str_tolower(additional_hdr[i].header_name)) { |
| 52 | hdr[k] := additional_hdr[i]; |
| 53 | updated := true; |
| 54 | } |
| 55 | } |
| 56 | if (updated == false) { |
| 57 | hdr := hdr & { additional_hdr[i] }; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return hdr; |
| 62 | } |
| 63 | |
| 64 | template (value) HeaderLine ts_HeaderLine(charstring header_name, charstring header_value) := { |
| 65 | header_name := header_name, |
| 66 | header_value := header_value |
| 67 | } |
| 68 | |
| 69 | function f_ts_HTTP_Header(template (omit) charstring body := omit, |
| 70 | template (omit) charstring host := omit, |
| 71 | HeaderLines custom_hdr := { }) |
| 72 | return template (value) HeaderLines { |
| 73 | var HeaderLines hdr := { }; |
| 74 | |
| 75 | /* Build default header */ |
| 76 | if (not istemplatekind(host, "omit")) { |
| 77 | hdr := hdr & {valueof(ts_HeaderLine("Host", valueof(host)))}; |
| 78 | } |
| 79 | hdr := hdr & {{ header_name := "Content-Type", header_value := "application/json" }}; |
| 80 | if (not istemplatekind(body, "omit")) { |
| 81 | hdr := hdr & {valueof(ts_HeaderLine("Content-Length", int2str(lengthof(body))))}; |
| 82 | } |
| 83 | |
| 84 | return f_overlay_HTTP_Header(hdr, custom_hdr); |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Philipp Maier | ef20fa0 | 2024-03-26 15:50:22 +0100 | [diff] [blame^] | 87 | function f_ts_body_or_empty(template (omit) charstring body) return template (value) charstring { |
| 88 | if (istemplatekind(body, "omit")) { |
| 89 | return ""; |
| 90 | } |
| 91 | return body; |
| 92 | } |
| 93 | |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 94 | template (value) HTTPMessage ts_HTTP_Req(charstring url, |
| 95 | charstring method := "GET", |
Philipp Maier | ef20fa0 | 2024-03-26 15:50:22 +0100 | [diff] [blame^] | 96 | template (omit) charstring body := omit, |
Philipp Maier | 4a19b47 | 2024-03-25 15:08:43 +0100 | [diff] [blame] | 97 | integer v_maj := 1, integer v_min := 1, |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 98 | charstring host, |
| 99 | HeaderLines custom_hdr := { }) := { |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 100 | request := { |
| 101 | client_id := omit, |
| 102 | method := method, |
| 103 | uri := url, |
| 104 | version_major := v_maj, |
| 105 | version_minor := v_min, |
Philipp Maier | ef20fa0 | 2024-03-26 15:50:22 +0100 | [diff] [blame^] | 106 | header := f_ts_HTTP_Header(body, host, custom_hdr), |
| 107 | body := f_ts_body_or_empty(body) |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | template HTTPMessage tr_HTTP_Resp(template integer sts := ?) := { |
| 112 | response := { |
| 113 | client_id := ?, |
| 114 | version_major := ?, |
| 115 | version_minor := ?, |
| 116 | statuscode := sts, |
| 117 | statustext := ?, |
| 118 | header := ?, |
| 119 | body := ? |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | template HTTPMessage tr_HTTP_Resp2xx := tr_HTTP_Resp((200..299)); |
| 124 | |
Philipp Maier | ef20fa0 | 2024-03-26 15:50:22 +0100 | [diff] [blame^] | 125 | function f_http_tx_request(charstring url, charstring method := "GET", template charstring body := omit, |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 126 | HeaderLines custom_hdr := { }) |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 127 | runs on http_CT { |
| 128 | HTTP.send(ts_HTTP_Connect(g_http_host, g_http_port)); |
| 129 | HTTP.receive(Connect_result:?); |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 130 | HTTP.send(ts_HTTP_Req(url, method, body, host := g_http_host & ":" & int2str(g_http_port), custom_hdr := custom_hdr)); |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | function f_http_rx_response(template HTTPMessage exp := tr_HTTP_Resp2xx, float tout := 2.0) |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 134 | runs on http_CT return HTTPMessage { |
| 135 | var HTTPMessage resp; |
Harald Welte | ad9d836 | 2021-02-22 10:23:00 +0100 | [diff] [blame] | 136 | timer T := tout; |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 137 | T.start; |
| 138 | alt { |
| 139 | [] HTTP.receive(exp) -> value resp { |
| 140 | setverdict(pass); |
| 141 | } |
| 142 | [] HTTP.receive(tr_HTTP_Resp) -> value resp { |
| 143 | setverdict(fail, "Unexpected HTTP response ", resp); |
| 144 | } |
| 145 | [] T.timeout { |
| 146 | setverdict(fail, "Timeout waiting for HTTP response"); |
| 147 | self.stop; |
| 148 | } |
| 149 | } |
| 150 | HTTP.send(ts_HTTP_Close); |
| 151 | return resp; |
| 152 | } |
| 153 | |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 154 | /* run a HTTP request and return the response */ |
| 155 | function f_http_transact(charstring url, charstring method := "GET", |
Philipp Maier | ef20fa0 | 2024-03-26 15:50:22 +0100 | [diff] [blame^] | 156 | template (omit) charstring body := omit, template HTTPMessage exp := tr_HTTP_Resp2xx, |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 157 | float tout := 2.0, HeaderLines custom_hdr := { }) |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 158 | runs on http_CT return HTTPMessage { |
Philipp Maier | 6406e6d | 2024-03-26 13:37:11 +0100 | [diff] [blame] | 159 | f_http_tx_request(url, method, body, custom_hdr); |
Harald Welte | 205b537 | 2021-02-22 09:16:21 +0100 | [diff] [blame] | 160 | return f_http_rx_response(exp, tout); |
| 161 | } |
| 162 | |
Harald Welte | 55e879c | 2021-02-19 13:16:28 +0100 | [diff] [blame] | 163 | } |