Skip to content

Fix misalignment between AdvancedSettings defaults and VCRHandler constructor #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions EasyVCR.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,49 @@ public async Task TestDefaultRequestMatching()
var cassette = TestUtils.GetCassette("test_default_request_matching");
cassette.Erase(); // Erase cassette before recording

const string postUrl = "http://httpbin.org/post";
const string postUrl1 = "https://httpbin.org/post?num=1";
const string postUrl2 = "https://httpbin.org/post?num=2";
var postBody = new StringContent("{\"key\":\"value\"}");

// record cassette first
var client = HttpClients.NewHttpClient(cassette, Mode.Record);
var response = await client.PostAsync(postUrl1, postBody);

// check that the request was not matched (should be a live call)
Assert.IsNotNull(response);
Assert.IsFalse(Utilities.ResponseCameFromRecording(response));

// replay cassette
client = HttpClients.NewHttpClient(cassette, Mode.Replay);
response = await client.PostAsync(postUrl1, postBody);

// check that the request was matched
Assert.IsNotNull(response);
Assert.IsTrue(Utilities.ResponseCameFromRecording(response));

// check that the request was not matched (should be a live call)
await Assert.ThrowsExceptionAsync<VCRException>(() => client.PostAsync(postUrl2, postBody));
}

[TestMethod]
public async Task TestDefaultRequestMatchingWithAdvancedSettings()
{
// test that match by method and url works
var cassette = TestUtils.GetCassette("test_default_request_matching");
cassette.Erase(); // Erase cassette before recording

const string postUrl1 = "https://httpbin.org/post?num=1";
const string postUrl2 = "https://httpbin.org/post?num=2";
var postBody = new StringContent("{\"key\":\"value\"}");

// record cassette first
var client = HttpClients.NewHttpClient(cassette, Mode.Record, new AdvancedSettings
{
MatchRules = MatchRules.Default // doesn't really matter for initial record
});
var response = await client.PostAsync(postUrl, postBody);
var response = await client.PostAsync(postUrl1, postBody);

// check that the request body was not matched (should be a live call)
// check that the request was not matched (should be a live call)
Assert.IsNotNull(response);
Assert.IsFalse(Utilities.ResponseCameFromRecording(response));

Expand All @@ -234,11 +266,15 @@ public async Task TestDefaultRequestMatching()
{
MatchRules = MatchRules.Default
});
response = await client.PostAsync(postUrl, postBody);
response = await client.PostAsync(postUrl1, postBody);

// check that the request body was matched
// check that the request was matched
Assert.IsNotNull(response);
Assert.IsTrue(Utilities.ResponseCameFromRecording(response));

// check that the request was not matched (should be a live call)
await Assert.ThrowsExceptionAsync<VCRException>(() => client.PostAsync(postUrl2, postBody));

}

[TestMethod]
Expand Down Expand Up @@ -458,7 +494,7 @@ public async Task TestMatchNonJsonBody()
var cassette = TestUtils.GetCassette("test_match_non_json_body");
cassette.Erase(); // Erase cassette before recording

const string url = "http://httpbin.org/post";
const string url = "https://httpbin.org/post";
var postData = HttpUtility.UrlEncode($"param1=name&param2=age", Encoding.UTF8);
var data = Encoding.UTF8.GetBytes(postData);
var content = new ByteArrayContent(data);
Expand Down Expand Up @@ -607,7 +643,7 @@ public async Task TestNestedCensoring()
var cassette = TestUtils.GetCassette("test_nested_censoring");
cassette.Erase(); // Erase cassette before recording

const string postUrl = "http://httpbin.org/post";
const string postUrl = "https://httpbin.org/post";
var postBody = new StringContent("{\r\n \"array\": [\r\n \"array_1\",\r\n \"array_2\",\r\n \"array_3\"\r\n ],\r\n \"dict\": {\r\n \"nested_array\": [\r\n \"nested_array_1\",\r\n \"nested_array_2\",\r\n \"nested_array_3\"\r\n ],\r\n \"nested_dict\": {\r\n \"nested_dict_1\": {\r\n \"nested_dict_1_1\": {\r\n \"nested_dict_1_1_1\": \"nested_dict_1_1_1_value\"\r\n }\r\n },\r\n \"nested_dict_2\": {\r\n \"nested_dict_2_1\": \"nested_dict_2_1_value\",\r\n \"nested_dict_2_2\": \"nested_dict_2_2_value\"\r\n }\r\n },\r\n \"dict_1\": \"dict_1_value\",\r\n \"null_key\": null\r\n }\r\n}");
// set up advanced settings
const string censorString = "censored-by-test";
Expand All @@ -632,7 +668,7 @@ public async Task TestStrictRequestMatching()
var cassette = TestUtils.GetCassette("test_strict_request_matching");
cassette.Erase(); // Erase cassette before recording

const string postUrl = "http://httpbin.org/post";
const string postUrl = "https://httpbin.org/post";
var postBody = new StringContent("{\n \"address\": {\n \"name\": \"Jack Sparrow\",\n \"company\": \"EasyPost\",\n \"street1\": \"388 Townsend St\",\n \"street2\": \"Apt 20\",\n \"city\": \"San Francisco\",\n \"state\": \"CA\",\n \"zip\": \"94107\",\n \"country\": \"US\",\n \"phone\": \"5555555555\"\n }\n}");

// record cassette first
Expand Down
2 changes: 1 addition & 1 deletion EasyVCR.Tests/FakeDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static string GetIPAddressDataUrl()

public static string GetPostManPostEchoServiceUrl()
{
return "http://httpbin.org/post";
return "https://httpbin.org/post";
}
}
}
2 changes: 1 addition & 1 deletion EasyVCR/Handlers/VCRHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private VCRHandler(Cassette cassette, Mode mode, AdvancedSettings? advancedSetti
_mode = mode;
_censors = advancedSettings?.Censors ?? new Censors();
_interactionConverter = advancedSettings?.InteractionConverter ?? new DefaultInteractionConverter();
_matchRules = advancedSettings?.MatchRules ?? new MatchRules();
_matchRules = advancedSettings?.MatchRules ?? MatchRules.Default;
_useOriginalDelay = advancedSettings?.SimulateDelay ?? false;
_delay = advancedSettings?.ManualDelayTimeSpan ?? TimeSpan.Zero;
_validTimeFrame = advancedSettings?.ValidTimeFrame ?? TimeFrame.Forever;
Expand Down