RestSharp with TestServer
See original GitHub issueExpected Behavior
Get back a valid response from a controller endpoint using RestTest
Actual Behavior
I always get back a 404.
Steps to Reproduce the Problem
Example call with RestSharp
public async Task<Example> GetExample(Guid exampleId)
{
this._restClient = new RestClient("http://localhost");
Example example = null;
RestRequest request = new RestRequest("api/example/{id}");
request.Method = Method.GET;
request.AddUrlSegment("id", exampleId);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
var response = this._restClient.Execute<Example>(request);
example = response.Data;
return example;
}
Working example with HttpClient
TestServer server = new TestServer(
new WebHostBuilder()
.UseStartup<Startup>());
HttpClient client = server.CreateClient();
var exampleId= new Guid("e2e8e066-2586-446a-9668-099eeb3e14ff");
var response = await client.GetAsync($"/api/example/{exampleId}");
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
var example= JsonConvert.DeserializeObject<Example>(responseJson);
Specifications
- Version: 106.1.0
- Platform: Windows 10
- Subsystem:
StackTrace
A 404 is returned and a break point on the Controller is never it when using RestSharp.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
RESTful API testing in C# with RestSharp
Some really basic checks RestSharp is available as a NuGet package, which makes it really easy to add to your C# project. ·...
Read more >How to mock a OWIN Testserver with RestSharp?
I changed the mock rest Client to work with Post/Put/Delete methods too. It is not 100% complete (missing auth, Cookies, files etc.) ...
Read more >Executable Tutorial: API Testing using RestSharp And SpecFlow
Overall RestSharp is a constantly updated open-source library, which provides an easy way to test .net-based applications. SpecFlow: Specflow is an open-source ...
Read more >Automated testing REST API using RestSharp and .NET Core
Moving testing focus from UI to REST API has in my opinion two main and obvious advantages - stability and speed.
Read more >Implementing RestSharp in REST API automated tests
Hi good sir, I would like to know if Restsharp can be implemented for load testing? Excuse me if my question has an...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Unfortunately, the
TestServer.CreateClient
produces an instance ofHttpClient
only. The only possible solution is to write something similar, which is quite a lot of work.Thanks @alexeyzimarev. We moved away from RestSharp and switched to HttpClient. Some test projects still reference RestSharp, I will try it out. 👍🏼