Skip to content

Commit 2e75325

Browse files
committed
Added two tests for PUT
1 parent 3e36e84 commit 2e75325

File tree

4 files changed

+88
-15
lines changed

4 files changed

+88
-15
lines changed

test/RestSharp.IntegrationTests/AsyncTests.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net;
22
using RestSharp.IntegrationTests.Fixtures;
3-
using RestSharp.Tests.Shared.Fixtures;
43

54
namespace RestSharp.IntegrationTests;
65

@@ -63,18 +62,6 @@ public async Task Can_Timeout_GET_Async() {
6362
Assert.Equal(ResponseStatus.TimedOut, response.ResponseStatus);
6463
}
6564

66-
[Fact]
67-
public async Task Can_Timeout_PUT_Async() {
68-
var request = new RestRequest("timeout", Method.Put).AddBody("Body_Content");
69-
70-
// Half the value of ResponseHandler.Timeout
71-
request.Timeout = 200;
72-
73-
var response = await _client.ExecuteAsync(request);
74-
75-
Assert.Equal(ResponseStatus.TimedOut, response.ResponseStatus);
76-
}
77-
7865
[Fact]
7966
public async Task Handles_GET_Request_Errors_Async() {
8067
var request = new RestRequest("status?code=404");

test/RestSharp.IntegrationTests/Fixtures/TestServer.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Text.Json;
12
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.Http;
45
using Microsoft.Extensions.Logging;
6+
using RestSharp.Tests.Shared.Extensions;
57

68
namespace RestSharp.IntegrationTests.Fixtures;
79

@@ -29,15 +31,27 @@ public HttpServer(ITestOutputHelper output = null) {
2931

3032
builder.WebHost.UseUrls(Address);
3133
_app = builder.Build();
34+
35+
var jsonOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
36+
37+
// GET
3238
_app.MapGet("success", () => new TestResponse { Message = "Works!" });
3339
_app.MapGet("echo", (string msg) => msg);
3440
_app.MapGet("timeout", async () => await Task.Delay(2000));
3541
_app.MapPut("timeout", async () => await Task.Delay(2000));
3642
// ReSharper disable once ConvertClosureToMethodGroup
3743
_app.MapGet("status", (int code) => Results.StatusCode(code));
38-
3944
_app.MapGet("headers", HandleHeaders);
4045

46+
// PUT
47+
_app.MapPut(
48+
"content",
49+
async context => {
50+
var content = await context.Request.Body.StreamToStringAsync();
51+
await context.Response.WriteAsync(content);
52+
}
53+
);
54+
4155
IResult HandleHeaders(HttpContext ctx) {
4256
var response = ctx.Request.Headers.Select(x => new TestServerResponse(x.Key, x.Value));
4357
return Results.Ok(response);
@@ -54,4 +68,6 @@ public async Task Stop() {
5468
}
5569
}
5670

57-
public record TestServerResponse(string Name, string Value);
71+
record TestServerResponse(string Name, string Value);
72+
73+
record ContentResponse(string Content);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
using System.Text.Json;
17+
using RestSharp.IntegrationTests.Fixtures;
18+
19+
namespace RestSharp.IntegrationTests;
20+
21+
[Collection(nameof(TestServerCollection))]
22+
public class PutTests {
23+
readonly ITestOutputHelper _output;
24+
readonly RestClient _client;
25+
26+
static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web);
27+
28+
public PutTests(TestServerFixture fixture, ITestOutputHelper output) {
29+
_output = output;
30+
_client = new RestClient(fixture.Server.Url);
31+
}
32+
33+
[Fact]
34+
public async Task Should_put_json_body() {
35+
var body = new TestRequest("foo", 100);
36+
var request = new RestRequest("content").AddJsonBody(body);
37+
var response = await _client.PutAsync(request);
38+
39+
var expected = JsonSerializer.Serialize(body, Options);
40+
response!.Content.Should().Be(expected);
41+
}
42+
43+
[Fact]
44+
public async Task Should_put_json_body_using_extension() {
45+
var body = new TestRequest("foo", 100);
46+
var response = await _client.PutJsonAsync<TestRequest, TestRequest>("content", body);
47+
response.Should().BeEquivalentTo(response);
48+
}
49+
50+
[Fact]
51+
public async Task Can_Timeout_PUT_Async() {
52+
var request = new RestRequest("timeout", Method.Put).AddBody("Body_Content");
53+
54+
// Half the value of ResponseHandler.Timeout
55+
request.Timeout = 200;
56+
57+
var response = await _client.ExecuteAsync(request);
58+
59+
Assert.Equal(ResponseStatus.TimedOut, response.ResponseStatus);
60+
}
61+
62+
}
63+
64+
record TestRequest(string Data, int Number);

test/RestSharp.Tests.Shared/Extensions/StreamExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ public static string StreamToString(this Stream stream) {
1414

1515
return streamReader.ReadToEnd();
1616
}
17+
18+
public static async Task<string> StreamToStringAsync(this Stream stream) {
19+
using var streamReader = new StreamReader(stream);
20+
21+
return await streamReader.ReadToEndAsync();
22+
}
1723
}

0 commit comments

Comments
 (0)