@@ -87,8 +87,70 @@ defmodule RigTests.Proxy.ResponseFrom.AsyncHttpTest do
8787 # ...the connection is closed and the status is OK:
8888 assert res_status == 201
8989 # ...extra headers are present
90- assert Enum . member? ( headers , { "content-type" , "application/json; charset=utf-8" } )
90+ assert Enum . member? ( headers , { "content-type" , "application/json;charset=utf-8" } )
9191 # ...but the client got the response sent to the HTTP internal endpoint:
9292 assert Jason . decode! ( res_body ) == async_response
9393 end
94+
95+ test_with_server "Given response_from is set to http_async and response code is incorrect, the calling service should receive 400 and originating request should timeout." do
96+ test_name = "proxy-http-response-from-http-internal-binary-timeout"
97+
98+ api_id = "mock-#{ test_name } -api"
99+ endpoint_id = "mock-#{ test_name } -endpoint"
100+ endpoint_path = "/#{ endpoint_id } "
101+ sync_response = % { "this response" => "the client never sees this response" }
102+ async_response = % { "message" => "this is the async response that reaches the client instead" }
103+
104+ route ( endpoint_path , fn % { query: % { "correlation" => correlation_id } } ->
105+ % Plug.Conn { status: res_status , resp_body: res_body } =
106+ build_conn ( )
107+ |> put_req_header ( "rig-correlation" , correlation_id )
108+ |> put_req_header ( "rig-response-code" , "abc201" )
109+ |> put_req_header ( "content-type" , "application/json;charset=utf-8" )
110+ |> post ( "/v3/responses" , Jason . encode! ( async_response ) )
111+
112+ assert res_status == 400
113+ assert res_body == "Failed to parse request body: {:error, {:not_an_integer, \" abc201\" }}"
114+
115+ Response . ok! ( sync_response , % { "content-type" => "application/json" } )
116+ end )
117+
118+ # We register the endpoint with the proxy:
119+ rig_api_url = "http://localhost:#{ @ api_port } /v3/apis"
120+ rig_proxy_url = "http://localhost:#{ @ proxy_port } "
121+
122+ body =
123+ Jason . encode! ( % {
124+ id: api_id ,
125+ name: "Mock API" ,
126+ version_data: % {
127+ default: % {
128+ endpoints: [
129+ % {
130+ id: endpoint_id ,
131+ method: "GET" ,
132+ path: endpoint_path ,
133+ response_from: "http_async"
134+ }
135+ ]
136+ }
137+ } ,
138+ proxy: % {
139+ target_url: "localhost" ,
140+ port: FakeServer . port ( )
141+ }
142+ } )
143+
144+ headers = [ { "content-type" , "application/json" } ]
145+ HTTPoison . post! ( rig_api_url , body , headers )
146+
147+ # The client calls the proxy endpoint:
148+ request_url = rig_proxy_url <> endpoint_path
149+
150+ assert catch_error ( HTTPoison . get! ( request_url ) ) == % HTTPoison.Error { id: nil , reason: :timeout }
151+
152+ # Now we can assert that...
153+ # ...the fake backend service has been called:
154+ assert FakeServer . hits ( ) == 1
155+ end
94156end
0 commit comments