From 9751d52140722056c380e6e00f7788be27e6a571 Mon Sep 17 00:00:00 2001 From: Marcel Date: Wed, 28 Jan 2026 12:48:53 +0100 Subject: [PATCH 1/3] Make sure to match only on chosen matchers Because: When doing soap requests in tests we often find the same endpoint to be used for multiple calls. The only difference here would be the body that we send to that endpoint. The body was already part of the matchers so adding it works, but for expect_request!'s where the body was omitted an :any was used and thus it would always match. This resulted into some tests that used this library not to find the right expectation for their request. We've now changed the match_score so that any's will be excluded from the match score. So that a closer match with a more specific target will be used over others. --- lib/http_ex/backend/mock/expectation.ex | 3 ++- test/http_ex/backend/mock_test.exs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/http_ex/backend/mock/expectation.ex b/lib/http_ex/backend/mock/expectation.ex index c1d696d..1d66091 100644 --- a/lib/http_ex/backend/mock/expectation.ex +++ b/lib/http_ex/backend/mock/expectation.ex @@ -836,7 +836,8 @@ defmodule HTTPEx.Backend.Mock.Expectation do fn {expectation, match_result} -> {true, fields, _misses, _vars} = match_result - match_score = length(fields) + match_score = + fields |> Enum.reject(&(expectation.matchers[&1] == :any)) |> length() expects_calls? = expectation.type == :assertion && expectation.min_calls >= 1 diff --git a/test/http_ex/backend/mock_test.exs b/test/http_ex/backend/mock_test.exs index 28c4774..91a7323 100644 --- a/test/http_ex/backend/mock_test.exs +++ b/test/http_ex/backend/mock_test.exs @@ -88,6 +88,29 @@ defmodule HTTPEx.Backend.MockTest do end end + test "OK, using the same endpoint for multiple request but with different body's" do + Mock.expect_request!( + endpoint: "http://www.example.com/soap", + body: "can't find this one", + response: %{status: 200, body: "OK"} + ) + + Mock.expect_request!( + endpoint: "http://www.example.com/soap", + body: "find_this_one", + expect_body: "find_this_one", + response: %{status: 200, body: "OK"} + ) + + assert Mock.request(%Request{ + client: :httpoison, + url: "http://www.example.com/soap", + method: :get, + body: "find_this_one" + }) == + {:ok, %HTTPoison.Response{status_code: 200, body: "OK", headers: []}} + end + test "max calls reached" do Mock.expect_request!( endpoint: "http://www.example.com", From de0bec098205922aba46be2ec95727eb3ff25fdf Mon Sep 17 00:00:00 2001 From: Marcel Date: Wed, 28 Jan 2026 15:04:05 +0100 Subject: [PATCH 2/3] Fix tests that should always have been faulty --- lib/http_ex/backend/mock/expectation.ex | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/http_ex/backend/mock/expectation.ex b/lib/http_ex/backend/mock/expectation.ex index 1d66091..29b7343 100644 --- a/lib/http_ex/backend/mock/expectation.ex +++ b/lib/http_ex/backend/mock/expectation.ex @@ -770,10 +770,10 @@ defmodule HTTPEx.Backend.Mock.Expectation do ...> } ...> ) ...> - ...> match == expectation_1 - true + ...> match + expectation_2 iex> vars - %{} + %{"api_version" => "v1"} iex> {:ok, match, vars} = ...> Expectation.find_matching_expectation( ...> expectations, @@ -785,8 +785,8 @@ defmodule HTTPEx.Backend.Mock.Expectation do ...> } ...> ) ...> - ...> match == expectation_1 - true + ...> match + expectation_1 iex> vars %{} From 92d62a061148c2328b8df804192cf391387c76df Mon Sep 17 00:00:00 2001 From: Marcel Date: Wed, 28 Jan 2026 15:11:20 +0100 Subject: [PATCH 3/3] Bump the version --- CHANGELOG.md | 7 +++++++ README.md | 2 +- mix.exs | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc9c92b..b5760de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ ### Bug Fixes +- Fix expect_request! when multiple requests are made to the same URL with different bodys. + When a `:body` matcher is used, the matcher is now prefering that over Expectations that omitted this. + +## v0.2.5 (2025-08-15) + +### Bug Fixes + - Fixed `http_client` option not being allowed in `expect_request!` ## v0.2.4 (2025-08-15) diff --git a/README.md b/README.md index 58fea6c..35055a9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ by adding `http_ex` to your list of dependencies in `mix.exs`: ```elixir def deps do [ - {:http_ex, "~> 0.2.5"} + {:http_ex, "~> 0.2.6"} ] end ``` diff --git a/mix.exs b/mix.exs index 20b2122..5604b67 100644 --- a/mix.exs +++ b/mix.exs @@ -16,7 +16,7 @@ defmodule HttpEx.MixProject do source_url: "https://github.com/wuunder/http_ex", start_permanent: Mix.env() == :prod, test_coverage: [tool: ExCoveralls], - version: "0.2.5" + version: "0.2.6" ] end