question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RestSharp with TestServer

See original GitHub issue

Expected 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:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
alexeyzimarevcommented, Jan 20, 2019

Unfortunately, the TestServer.CreateClient produces an instance of HttpClient only. The only possible solution is to write something similar, which is quite a lot of work.

0reactions
jacodvcommented, Jan 14, 2022

Thanks @alexeyzimarev. We moved away from RestSharp and switched to HttpClient. Some test projects still reference RestSharp, I will try it out. 👍🏼

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found