Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added default parameters to the request. They got missing somehow. (#…
- Loading branch information
1 parent
205d170
commit 3a7178b
Showing
7 changed files
with
91 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,62 @@ | ||
using System.Net; | ||
using RestSharp.Tests.Integrated.Fixtures; | ||
using RestSharp.Tests.Integrated.Server; | ||
using RestSharp.Tests.Shared.Fixtures; | ||
|
||
namespace RestSharp.Tests.Integrated; | ||
|
||
public class HttpHeadersTests : CaptureFixture { | ||
[Collection(nameof(TestServerCollection))] | ||
public class HttpHeadersTests { | ||
readonly ITestOutputHelper _output; | ||
readonly RestClient _client; | ||
|
||
public HttpHeadersTests(ITestOutputHelper output) => _output = output; | ||
public HttpHeadersTests(TestServerFixture fixture, ITestOutputHelper output) { | ||
_output = output; | ||
_client = new RestClient(new RestClientOptions(fixture.Server.Url) { ThrowOnAnyError = true }); | ||
} | ||
|
||
[Fact] | ||
public async Task Ensure_headers_correctly_set_in_the_hook() { | ||
const string headerName = "HeaderName"; | ||
const string headerValue = "HeaderValue"; | ||
|
||
using var server = SimpleServer.Create(Handlers.Generic<RequestHeadCapturer>()); | ||
|
||
// Prepare | ||
var client = new RestClient(server.Url); | ||
|
||
var request = new RestRequest(RequestHeadCapturer.Resource) { | ||
var request = new RestRequest("/headers") { | ||
OnBeforeRequest = http => { | ||
http.Headers.Add(headerName, headerValue); | ||
return default; | ||
} | ||
}; | ||
|
||
// Run | ||
await client.ExecuteAsync(request); | ||
var response = await _client.ExecuteAsync<TestServerResponse[]>(request); | ||
|
||
// Assert | ||
RequestHeadCapturer.CapturedHeaders[headerName].Should().Be(headerValue); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
var header = response.Data!.First(x => x.Name == headerName); | ||
header.Should().NotBeNull(); | ||
header.Value.Should().Be(headerValue); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_use_both_default_and_request_headers() { | ||
var defaultHeader = new Header("defName", "defValue"); | ||
var requestHeader = new Header("reqName", "reqValue"); | ||
|
||
_client.AddDefaultHeader(defaultHeader.Name, defaultHeader.Value); | ||
|
||
var request = new RestRequest("/headers") | ||
.AddHeader(requestHeader.Name, requestHeader.Value); | ||
|
||
var response = await _client.ExecuteAsync<TestServerResponse[]>(request); | ||
CheckHeader(defaultHeader); | ||
CheckHeader(requestHeader); | ||
|
||
void CheckHeader(Header header) { | ||
var h = response.Data!.First(x => x.Name == header.Name); | ||
h.Should().NotBeNull(); | ||
h.Value.Should().Be(header.Value); | ||
} | ||
} | ||
|
||
record Header(string Name, string Value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/RestSharp.Tests.Integrated/Server/Handlers/FormRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace RestSharp.Tests.Integrated.Server.Handlers; | ||
|
||
public static class FormRequestHandler { | ||
public static IResult HandleForm(HttpContext ctx) { | ||
var response = ctx.Request.Form.Select( | ||
x => new TestServerResponse(x.Key, x.Value) | ||
); | ||
return Results.Ok(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters