I am getting "Cannot access a closed Stream" when unit testing a Nancy web app.
My module is as follow:
public class MainModule : NancyModule
{
public MainModule()
{
Get["/{Name}"] = p =>
{
var command = this.Bind<MainCommand>();
return Response.AsJson(command.ExecuteGetMessage());
};
}
}
And my test is:
[Test]
public void Should_return_welcome_message()
{
// When
var bootstrapper = new DefaultNancyBootstrapper();
var browser = new Browser(bootstrapper);
BrowserResponse browserResponse = browser.Get("/", with =>
{
with.HttpRequest();
});
Console.WriteLine(browserResponse.StatusCode);
Console.WriteLine(browserResponse.Body.ToString());
// Then
Assert.AreEqual(HttpStatusCode.OK,browserResponse.StatusCode);
}
UPDATE: I am getting StatusCode = NotFound and the exception happens when trying to access browserResponse.Body.
I had a look at the Nancy forum and also here at StackOverflow.
I tried this solution: Nancy test doesn't find route in other assembly
but still not working.
When I run the test in debug mode I see that my module is been called but I still cant check the returned value.
What should I do in order to get it working?
Thanks
Ademar
You are getting a NotFound response because the route you have defined is different from the route you have called.
You are calling / in your test, but the module has a route of /{Name}.
The exception is because there is no body with a NotFound response.
Update your test to something like:
var bootstrapper = new ConfigurableBootstrapper(c=>c.Module<MainModule>());
var browser = new Browser(bootstrapper);
BrowserResponse browserResponse = browser.Get("/ademar", with =>
{
with.HttpRequest();
});
* Updated to include source from the comment *
Related
I'm working on a project where we have to develop a web API with ASP .NET Core 3.x. So far, so good, it is running well. Now, I'm writing some integration tests for this web API and I have some trouble to get the tests for everything else than GET request to work.
We're using the Clean Architecture from Jason Taylor. This means we have a core project with all our request handler, a domain project with all database entities and a presentation project for the API controllers. We use MediatR and dependency injection for the communication between these projects.
Now, we have the problem that the body data of the reuqest doesn't reach the controller.
This is how the Update method in the controller looks like:
[ApiController]
[Route("api/[controller]/[action]")]
public abstract class BaseController : ControllerBase
{
private IMediator _mediator;
protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>();
}
public class FavoriteController : BaseController
{
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Update(long id, UpdateFavoriteCommand command)
{
if (command == null || id != command.Id)
{
return BadRequest();
}
// Sends the request to the corresponding IRequestHandler
await Mediator.Send(command);
return NoContent();
}
}
We use xUnit.net as test framework.
For the integration tests, we're using an InMemory SQLite database which is setup in a fixture class.
The test looks like the following:
public class UpdateFavoritesTestSqlite : IClassFixture<WebApplicationFactoryWithInMemorySqlite<Startup>>
{
private readonly WebApplicationFactoryWithInMemorySqlite<Startup> _factory;
private readonly string _endpoint;
public UpdateFavoritesTestSqlite(WebApplicationFactoryWithInMemorySqlite<Startup> factory)
{
_factory = factory;
_endpoint = "api/Favorite/Update/";
}
[Fact]
public async Task UpdateFavoriteDetail_WithFullUpdate_ShouldUpdateCorrectly()
{
// Arange
var client = _factory.CreateClient(); // WebApplicationFactory.CreateClient()
var command = new UpdateFavoriteCommand
{
Id = 5,
Caption = "caption new",
FavoriteName = "a new name",
Public = true
};
// Convert to JSON
var jsonString = JsonConvert.SerializeObject(command);
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
var stringUri = client.BaseAddress + _endpoint + command.Id;
var uri = new Uri(stringUri);
// Act
var response = await client.PutAsync(uri, httpContent);
response.EnsureSuccessStatusCode();
httpContent.Dispose();
// Assert
response.StatusCode.ShouldBe(HttpStatusCode.NoContent);
}
}
If we run the test, we get an 400 Bad Request error.
If we run the test in Debug mode, we can see that the code throws a custom ValidationException because of a model state error. This is configured in the DependencyInjection of the presentation project:
services
.AddControllers()
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
var failures = context.ModelState.Keys
.Where(k => ModelValidationState.Invalid.Equals(context.ModelState[k].ValidationState))
.ToDictionary(k => k, k => (IEnumerable<string>)context.ModelState[k].Errors.Select(e => e.ErrorMessage).ToList());
throw new ValidationException(failures);
};
})
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<IWebApiDbContext>());
The failures object contains one error which says:
The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
Here is a screenshot: Visual Studio in Debugging mode with json error.
In one article in stackoverflow I've read, that removing the [ApiController] class attribute can result in a more detailed error description. During debugging again the test and setting a breakpoint int the Update method from the FavoriteController at the line with await Mediator.Send(command);, I was able to see, that the command object arriving the Update method contains only null or default values, except the id, which was 5.
command
Caption null string
FavoriteName null string
Id 5 long
Public false bool
The most confusing (and frustrating) part is, that a manual test with swagger or postman are both successfull. The way I understand it, there has to be a problem during the integration test.
I hope, someone can help me and see what I'm missing. Could it be possible that there is something wrong with the HttpClient of the Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory?
We've found the problem in our LoggingMiddleware of the web api presentation project.
Before writing this question, we already hat a look at another article on stackoverflow:
ASP.NET MVC Core 3.0 - Why API Request from body keeps returning !ModelState.IsValid?
But we already had the request.Body.Seek(0, SeekOrigin.Begin); in our code. So, we thought that's it and it couldn't be the problem.
But now, we've found this article:
.net core 3.0 logging middleware by pipereader
Instead of reading the request body like this:
await request.Body.ReadAsync(buffer, 0, buffer.Length);
... where the stream is closed after read, we're using now the BodyReader as a stream and leave the stream open:
var stream = request.BodyReader.AsStream(true); // AsStream(true) to let stream open
await stream.ReadAsync(buffer, 0, buffer.Length);
request.Body.Seek(0, SeekOrigin.Begin);
So i have this ASP.NET Core on my local machine, i have installed the prerequisites and after running the application locally, the response was correct from the web browset that it was not found.
Okay, i am trying to invoked this API via Postman and i couldnt determine why i cant access it though i already checked the routings.
Below is the sample template
[HttpGet]
[Route("Details")]
public async Task<IActionResult> GetDetails(string value = null)
{
var response = new ListModelResponse<SomeModel>() as IListModelResponse<SomeModel>;
try
{
response.Model = await GetDetailsRepository
.GetDetailsSS(value)
.Select(item => item.ToViewModel())
.OrderBy(item => item.Name)
.ToListAsync();
}
catch (Exception ex)
{
response.DidError = true;
response.ErrorMessage = ex.Message;
}
return response.ToHttpResponse();
}
And in application insights of visual studio, i can see that it is invoking the API but it seems it can't get through.
Check this insights snippet
Other API's are working fine, it seems that i am missed something that i can't figure out right now.
For the routing i have this.
[Route("api/[controller]")]
I have also checked the Token and as i parsed it, i am getting the required info to access this API.
Thanks Lads!
It doesn't seem that you have the name of the controller in the request url.
api/[controller]/Details?value=CAT...
This error is due to the incorrect url present in the request. The correct URL has to be https://localhost:44309/api/your-controller-name/Details?value=CAT
ie. If the Controller name is ProductsController, then the URL has to be https://localhost:44309/api/Products/Details?value=CAT.
[Route("api/[controller]")]
public class ProductsController : Controller
{
[HttpPost("Details")]
public async Task<IActionResult> GetDetails(string value = null)
{
...
}
}
I am writing a nuget package that will consume a REST API I wrote. Im having some difficulty in calling it asynchronously using RestSharp.
I have the following controller method:
[HttpGet("{id}")]
public async Task<IActionResult> GetFeatureByIdAsync(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
ModelState.AddModelError("id-Null", "The id parameter cannot be null.");
return BadRequest(ModelState);
}
var feature = await _director.GetFeatureById(id);
if (feature == null)
return NotFound();
var model = new ViewModel<Feature>(feature);
return Ok(model);
}
I have the follow class library in a seperate solution:
public async Task<bool> IsSwitchEnabledAsync(string name)
{
string fullyFormedUrl = $"{_url}/{name}";
var client = new RestClient(fullyFormedUrl) { Encoding = Encoding.UTF8 };
var request = new RestRequest(Method.GET) { RequestFormat = DataFormat.Json };
var result = await client.ExecuteTaskAsync<FeatureViewModel>(request);
var message = $"Response: {result.StatusCode}, {result.ResponseStatus}, " +
$"\n\nError: {result.ErrorException}, {result.ErrorMessage} " +
$"\n\nData: {result.Content}";
Debug.WriteLine(message);
return result.Data != null;
}
I have the following basic integration test sandbox:
[TestFixture]
public class Sandbox
{
private FeatureSwitch _sut;
[OneTimeSetUp]
public void Setup()
{
const string machineName = "localhost";
const int port = 5000;
_sut = new FeatureSwitch(machineName, port);
}
[Test]
public async Task ShouldReturnTrue()
{
var result = await _sut.IsSwitchEnabledAsync("Release_US501_AddUser");
result.Should().BeTrue();
}
}
I am looking for a little education on the best approach on how to call the API correctly? As my sandbox code is failing with an Internal Server 500 Error. This is because the api method is waiting for the nested async call to complete. So the initial request returns back to the sandbox code before the nested api call has a chance to complete.
The sandbox test code needs to wait for the API to return a result. At the moment it is not doing this.
So what is the correct approach here?
UPDATE: My issue was down to incorrect usage of a Mongo library method.
My issue came from a .chained method call on a Mongo library method:
I was trying to do the following:
Collection.Find(x => x.Name.ToLower().Equals(name.ToLower())
The code compiled but it was throwing an exception that VS studio 17 was not breaking on so i had no idea where the issue was occurring. With code lens turned on, I noticed that the failed requests highlighted in red. This opened an App Insights window which highlighted a break down of 4 InvalidOperationException() that had been thrown. That's how i discovered what the Internal Server Error 500 was about.
Anyways that's how i solved Async Web API problem - it was my first time using it on a web service so I had no idea what the expected default behaviour ought to be.
I would like to unit test my ValidationFeature rulesets in my ServiceStack project however the plugin is not being initialized when creating my appHost object.
Here is my original code to initialize the appHost.
[TestFixtureSetUp]
private void TestFixtureSetUp()
{
appHost = new BasicAppHost().Init();
appHost.Plugins.Add(new ValidationFeature());
var container = appHost.Container;
container.RegisterValidators(typeof(ApplicationValidator).Assembly);
container.RegisterAutoWiredAs<FakeRetailReferralRepository, IRetailReferralRepository>();
container.RegisterAutoWired<SubmitApplicationService>();
}
I've tried moving the Plugins.Add line in between the BasicAppHost constructor and Init() and that didn't work either. Is what I'm trying to do possible?
The validation feature is validated within the Request Pipeline so would typically require a full integration test to test it, i.e. using a self-host and service client.
You can still unit test a validator, but as validation occurs before the Service is called you would need to test the validator with the Request DTO directly instead of calling the Service, e.g:
using (var appHost = new BasicAppHost
{
ConfigureAppHost = host => {
host.Plugins.Add(new ValidationFeature());
},
ConfigureContainer = c => {
c.RegisterValidators(typeof(ApplicationValidator).Assembly);
}
}.Init())
{
var myValidator = appHost.TryResolve<IValidator<MyRequest>>();
var result = myValidator.Validate(new MyRequest { ... });
Assert.That(result.IsValid, Is.False);
Assert.That(result.Errors.Count, Is.EqualTo(1));
}
I'm writing unit tests in which I need a fake HttpContext. I used the HttpSimulator from Phil Haack (http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx).
But an function in the Sitecore api throws an exception on the following code:
request.Browser.Browser.IndexOf("IE", StringComparison.OrdinalIgnoreCase) > -1;
I debugged my code and the request.Browser.Browser is empty. I tried to fill the property with Moq but I get an exception. I also tried to add it as a header.
My code looks like this:
using (HttpSimulator simulator = new HttpSimulator("/", "localpath"))
{
//NameValueCollection nvc = new NameValueCollection { { "User-Agent", "IE" } };
//simulator.SimulateRequest(new Uri("http://www.foo.bar"), HttpVerb.GET, nvc);
simulator.SimulateRequest();
var browserMock = new Mock<HttpBrowserCapabilities>();
browserMock.SetupAllProperties();
//browserMock.SetupProperty(b => b.Browser, "IE");
HttpContext.Current.Request.Browser = browserMock.Object;
}
Does anyone know how i can mock this property?
Actually this is possible - you can set the Browser property directly on the request:
httpSim.SimulateRequest(new Uri("http://www.test.com"));
HttpContext.Current.Request.Browser = new HttpBrowserCapabilities
{
Capabilities = new Dictionary<string,string>
{
{"majorversion", "8"},
{"browser", "IE"},
{"isMobileDevice","false"}
}
};
This can not be done, you will have to use the HttpContextBase e.d. classes in MVC. Turns out unit testing with web forms is messy(?)