diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-08-05 00:04:58 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-08-05 00:04:58 -0400 |
commit | ebaaddf076097268a3430728695eca146395165d (patch) | |
tree | 1d78104f85642111e0a1254940536eeaf2a34803 | |
parent | minor code improvements (diff) |
check that the server response has a JSON content-type
12 files changed, 21 insertions, 15 deletions
diff --git a/lib/polyjuice/client/endpoint.ex b/lib/polyjuice/client/endpoint.ex index b75ee6c..41c0a86 100644 --- a/lib/polyjuice/client/endpoint.ex +++ b/lib/polyjuice/client/endpoint.ex @@ -87,8 +87,14 @@ defmodule Polyjuice.Client.Endpoint do ) :: any def parse_response(%{} = endpoint_args, status_code, headers, body) when is_integer(status_code) and is_list(headers) and is_binary(body) do - # FIXME: check if content type is "application/json" - with {:ok, json} <- Jason.decode(body) do + # make sure it's JSON content + with {_, "application/json"} <- + Enum.find( + headers, + {nil, nil}, + fn {name, _} -> String.downcase(name, :ascii) == "content-type" end + ), + {:ok, json} <- Jason.decode(body) do case status_code do 200 -> Polyjuice.Client.Endpoint.BodyParser.parse(endpoint_args, json) diff --git a/test/polyjuice/client/endpoint/get_rooms_messages_test.exs b/test/polyjuice/client/endpoint/get_rooms_messages_test.exs index 1b27f09..6d2002c 100644 --- a/test/polyjuice/client/endpoint/get_rooms_messages_test.exs +++ b/test/polyjuice/client/endpoint/get_rooms_messages_test.exs @@ -37,7 +37,7 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsMessagesTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], "{}" ) == {:ok, %{}} diff --git a/test/polyjuice/client/endpoint/get_rooms_state_test.exs b/test/polyjuice/client/endpoint/get_rooms_state_test.exs index f6e0596..ec7911f 100644 --- a/test/polyjuice/client/endpoint/get_rooms_state_test.exs +++ b/test/polyjuice/client/endpoint/get_rooms_state_test.exs @@ -37,7 +37,7 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsStateTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], "[{\"type\": \"m.room.name\", \"content\": {\"name\": \"foo1\"}}]" ) == {:ok, [%{"type" => "m.room.name", "content" => %{"name" => "foo1"}}]} @@ -72,7 +72,7 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsStateTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], "{\"name\": \"foo1\"}" ) == {:ok, %{"name" => "foo1"}} diff --git a/test/polyjuice/client/endpoint/get_sync_test.exs b/test/polyjuice/client/endpoint/get_sync_test.exs index 939e0e2..ae03aed 100644 --- a/test/polyjuice/client/endpoint/get_sync_test.exs +++ b/test/polyjuice/client/endpoint/get_sync_test.exs @@ -32,7 +32,7 @@ defmodule Polyjuice.Client.Endpoint.GetSyncTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], "{}" ) == {:ok, %{}} diff --git a/test/polyjuice/client/endpoint/post_join_test.exs b/test/polyjuice/client/endpoint/post_join_test.exs index ec9b6a4..fef86b1 100644 --- a/test/polyjuice/client/endpoint/post_join_test.exs +++ b/test/polyjuice/client/endpoint/post_join_test.exs @@ -58,7 +58,7 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], ~s({"room_id":"!room"}) ) == {:ok, "!room"} @@ -93,7 +93,7 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], ~s({"room_id":"!room"}) ) == {:ok, "!room"} end diff --git a/test/polyjuice/client/endpoint/post_login_test.exs b/test/polyjuice/client/endpoint/post_login_test.exs index 85fc1ba..6bf7781 100644 --- a/test/polyjuice/client/endpoint/post_login_test.exs +++ b/test/polyjuice/client/endpoint/post_login_test.exs @@ -50,7 +50,7 @@ defmodule Polyjuice.Client.Endpoint.PostLoginTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], ~s({"user_id":"@alice:example.com","access_token":"1234567890","device_id":"ABCDEF","well_known":{"m.homeserver":{"base_url":"https://example.com"},"m.identity_server":{"base_url":"https://example.com"}}}) ) == { :ok, diff --git a/test/polyjuice/client/endpoint/post_logout_test.exs b/test/polyjuice/client/endpoint/post_logout_test.exs index b548d9f..9dde596 100644 --- a/test/polyjuice/client/endpoint/post_logout_test.exs +++ b/test/polyjuice/client/endpoint/post_logout_test.exs @@ -34,7 +34,7 @@ defmodule Polyjuice.Client.Endpoint.PostLogoutTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], "{}" ) == {:ok} diff --git a/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs b/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs index cebe89f..39a7453 100644 --- a/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs +++ b/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs @@ -38,7 +38,7 @@ defmodule Polyjuice.Client.Endpoint.PostRoomsReceiptTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], "{}" ) == {:ok} diff --git a/test/polyjuice/client/endpoint/post_user_filter_test.exs b/test/polyjuice/client/endpoint/post_user_filter_test.exs index 6d001cc..605834c 100644 --- a/test/polyjuice/client/endpoint/post_user_filter_test.exs +++ b/test/polyjuice/client/endpoint/post_user_filter_test.exs @@ -41,7 +41,7 @@ defmodule Polyjuice.Client.Endpoint.PostUserfilterTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], ~s({"filter_id":"abc"}) ) == {:ok, "abc"} diff --git a/test/polyjuice/client/endpoint/put_rooms_send_test.exs b/test/polyjuice/client/endpoint/put_rooms_send_test.exs index a05018e..5c3f2ef 100644 --- a/test/polyjuice/client/endpoint/put_rooms_send_test.exs +++ b/test/polyjuice/client/endpoint/put_rooms_send_test.exs @@ -42,7 +42,7 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsSendTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], "{\"event_id\": \"$foo1\"}" ) == {:ok, "$foo1"} diff --git a/test/polyjuice/client/endpoint/put_rooms_state_test.exs b/test/polyjuice/client/endpoint/put_rooms_state_test.exs index 8f2f5aa..04fa59e 100644 --- a/test/polyjuice/client/endpoint/put_rooms_state_test.exs +++ b/test/polyjuice/client/endpoint/put_rooms_state_test.exs @@ -41,7 +41,7 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsStateTest do assert Polyjuice.Client.Endpoint.Proto.transform_http_result( endpoint, 200, - [], + [{"Content-Type", "application/json"}], "{\"event_id\": \"$foo1\"}" ) == {:ok, "$foo1"} diff --git a/test/polyjuice/client/endpoint_test.exs b/test/polyjuice/client/endpoint_test.exs index 5c19a5f..e3a4f3a 100644 --- a/test/polyjuice/client/endpoint_test.exs +++ b/test/polyjuice/client/endpoint_test.exs @@ -23,7 +23,7 @@ defmodule Polyjuice.Client.EndpointTest do assert Polyjuice.Client.Endpoint.parse_response( %{}, 404, - [], + [{"Content-Type", "application/json"}], ~s({"errcode": "M_NOT_FOUND", "error": "not found"}) ) == {:error, 404, %{"errcode" => "M_NOT_FOUND", "error" => "not found"}} |