Skip to content

Commit

Permalink
Fix unit tests (#54)
Browse files Browse the repository at this point in the history
- Switch to httpbin to match other tests
- Manually install .NET Core 3.1 in GitHub Runner for CI
  • Loading branch information
Greybird authored Jan 26, 2024
1 parent 991546c commit 6b8f7ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ jobs:
- name: Install .NET SDK
uses: actions/setup-dotnet@v3
with:
# .NET 5 is deprecated and removed from GitHub Actions, we need to manually install it
# .NET Core 3.1 and .NET 5 are deprecated and removed from GitHub Actions, we need to manually install them
dotnet-version: |
3.1.x
5.x.x
7.x.x
Expand Down
14 changes: 7 additions & 7 deletions EasyVCR.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public async Task TestIgnoreElementsFailMatch()

// record baseline request first
var client = HttpClients.NewHttpClient(cassette, Mode.Record);
var _ = await client.PostAsync(FakeDataService.GetPreparedIPAddressDataUrl("json"), bodyData1);
var _ = await client.PostAsync(FakeDataService.GetPostManPostEchoServiceUrl(), bodyData1);

// try to replay the request with different body data
client = HttpClients.NewHttpClient(cassette, Mode.Replay, new AdvancedSettings
Expand All @@ -388,7 +388,7 @@ public async Task TestIgnoreElementsFailMatch()
});

// should fail since we're strictly in replay mode and there's no exact match
await Assert.ThrowsExceptionAsync<VCRException>(async () => await client.PostAsync(FakeDataService.GetPreparedIPAddressDataUrl("json"), bodyData2));
await Assert.ThrowsExceptionAsync<VCRException>(async () => await client.PostAsync(FakeDataService.GetPostManPostEchoServiceUrl(), bodyData2));
}

[TestMethod]
Expand All @@ -402,7 +402,7 @@ public async Task TestCustomMatchRule()

// record baseline request first
var client = HttpClients.NewHttpClient(cassette, Mode.Record);
var _ = await client.PostAsync(FakeDataService.GetPreparedIPAddressDataUrl("json"), bodyData1);
var _ = await client.PostAsync(FakeDataService.GetPostManPostEchoServiceUrl(), bodyData1);

// try to replay the request with no custom match rule
client = HttpClients.NewHttpClient(cassette, Mode.Replay, new AdvancedSettings()
Expand All @@ -411,7 +411,7 @@ public async Task TestCustomMatchRule()
});

// should pass since it passes the default match rules
await client.PostAsync(FakeDataService.GetPreparedIPAddressDataUrl("json"), bodyData1);
await client.PostAsync(FakeDataService.GetPostManPostEchoServiceUrl(), bodyData1);

// try to replay the request with a custom match rule
client = HttpClients.NewHttpClient(cassette, Mode.Replay, new AdvancedSettings
Expand All @@ -420,7 +420,7 @@ public async Task TestCustomMatchRule()
});

// should fail since the custom match rule always returns false and there's never a match
await Assert.ThrowsExceptionAsync<VCRException>(async () => await client.PostAsync(FakeDataService.GetPreparedIPAddressDataUrl("json"), bodyData2));
await Assert.ThrowsExceptionAsync<VCRException>(async () => await client.PostAsync(FakeDataService.GetPostManPostEchoServiceUrl(), bodyData2));
}

[TestMethod]
Expand All @@ -434,7 +434,7 @@ public async Task TestIgnoreElementsPassMatch()

// record baseline request first
var client = HttpClients.NewHttpClient(cassette, Mode.Record);
var _ = await client.PostAsync(FakeDataService.GetPreparedIPAddressDataUrl("json"), bodyData1);
var _ = await client.PostAsync(FakeDataService.GetPostManPostEchoServiceUrl(), bodyData1);

// try to replay the request with different body data, but ignoring the differences
var ignoreElements = new List<CensorElement>
Expand All @@ -447,7 +447,7 @@ public async Task TestIgnoreElementsPassMatch()
});

// should succeed since we're ignoring the differences
var response = await client.PostAsync(FakeDataService.GetPreparedIPAddressDataUrl("json"), bodyData2);
var response = await client.PostAsync(FakeDataService.GetPostManPostEchoServiceUrl(), bodyData2);
Assert.IsNotNull(response);
Assert.IsTrue(Utilities.ResponseCameFromRecording(response));
}
Expand Down
9 changes: 7 additions & 2 deletions EasyVCR.Tests/FakeDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,21 @@ public async Task<HttpResponseMessage> GetIPAddressDataRawResponse()
return await Client.GetAsync(GetPreparedIPAddressDataUrl(_format));
}

protected abstract IPAddressData Convert(string responseBody);

public static string GetPreparedIPAddressDataUrl(string? format)
{
return $"{GetIPAddressDataUrl()}?format={format}";
}

protected abstract IPAddressData Convert(string responseBody);

public static string GetIPAddressDataUrl()
{
return "https://api.ipify.org/";
}

public static string GetPostManPostEchoServiceUrl()
{
return "http://httpbin.org/post";
}
}
}

0 comments on commit 6b8f7ee

Please sign in to comment.