// ENDSAMPLE // SAMPLE: setting-request-headers public Task setting_request_headers(ISystemUnderTest system) { return(system.Scenario(_ => { _.Context.Request.Headers["foo"] = "bar"; })); }
public StepthroughSession(IProject project, ISystemUnderTest system, ISpecificationObserver observer, ExecutionPlan plan) { _project = project; _system = system; _observer = observer; _plan = plan; }
// ENDSAMPLE // SAMPLE: send-text public Task send_text(ISystemUnderTest system) { return(system.Scenario(_ => { _.Post.Text("some text").ToUrl("/textdata"); })); }
/// <summary> /// Shortcut to issue a POST with a Json serialized request body and a Json serialized /// response body /// </summary> /// <param name="system"></param> /// <param name="request"></param> /// <param name="url"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static ResponseExpression PostJson <T>(this ISystemUnderTest system, T request, string url) where T : class { return(new ResponseExpression(system, s => { s.Post.Json(request).ToUrl(url); })); }
// How can we do this in such a way that you can use StructureMap child containers // for test isolation? public static async Task <IScenarioResult> Scenario(this ISystemUnderTest system, Action <Scenario> configure) { using (var scope = system.Services.GetService <IServiceScopeFactory>().CreateScope()) { var scenario = new Scenario(system, scope); configure(scenario); scenario.Rewind(); try { await system.BeforeEach(scenario.Context).ConfigureAwait(false); await scenario.RunBeforeActions().ConfigureAwait(false); if (scenario.Context.Request.Path == null) { throw new InvalidOperationException("This scenario has no defined url"); } await system.Invoker(scenario.Context).ConfigureAwait(false); scenario.RunAssertions(); await scenario.RunAfterActions().ConfigureAwait(false); } finally { await system.AfterEach(scenario.Context).ConfigureAwait(false); } return(scenario); } }
// ENDSAMPLE // SAMPLE: setting-request-headers public Task setting_request_headers(ISystemUnderTest system) { return(system.Scenario(_ => { _.SetRequestHeader("foo", "bar"); })); }
public void Setup() { _mockMessageHandler = A.Fake <HttpMessageHandler>(); var httpClient = new HttpClient(_mockMessageHandler); _systemUnderTest = new SystemUnderTest(httpClient); }
public ScenarioContext() { host = new SystemUnderTest(new WebHostBuilder().Configure(app => { app.Run(router.Invoke); })); host.Urls = router; }
public ORMGraphQLTests() { builder = WebHost.CreateDefaultBuilder().UseStartup <Startup>().UseEnvironment("Testing"); sut = new SystemUnderTest(builder); var mutations = (ProfileLocationMutations)sut.Services.GetService(typeof(ProfileLocationMutations)); mutations.EnableAllMutations(); queryGenerator = new GraphQLQueryTestGenerator(); }
// ENDSAMPLE // SAMPLE: asserting-windows-auth-with-user public Task asserting_authentication_with_user(ISystemUnderTest system) { return(system.Scenario(_ => { var user = new ClaimsPrincipal(new ClaimsIdentity(Enumerable.Empty <Claim>(), "Windows")); _.WithWindowsAuthentication(user); _.Get.Url("/"); _.StatusCodeShouldBe(HttpStatusCode.Forbidden); })); }
// SAMPLE: asserting-windows-auth public Task asserting_authentication(ISystemUnderTest system) { return(system.Scenario(_ => { _.WithWindowsAuthentication(); _.Get.Url("/"); _.StatusCodeShouldBe(HttpStatusCode.Unauthorized); _.Header("www-authenticate").ShouldHaveValues("NTLM", "Negotiate"); })); }
public async Task run_simple_scenario_bootstrapped_by_Startup() { theSystem = JasperHttpTester.For <AlbaTargetApp2>(); await theSystem.Scenario(_ => { _.Get.Url("/"); _.StatusCodeShouldBeOk(); _.ContentShouldContain("Texas"); }); }
// SAMPLE: asserting-redirects public Task asserting_redirects(ISystemUnderTest system) { return(system.Scenario(_ => { // should redirect to the url _.RedirectShouldBe("/redirect"); // should redirect permanently to the url _.RedirectPermanentShouldBe("/redirect"); })); }
public async Task run_simple_scenario_that_uses_custom_services() { theSystem = JasperHttpTester.For <AlbaTargetApp>(); await theSystem.Scenario(_ => { _.Get.Url("/"); _.StatusCodeShouldBeOk(); _.ContentShouldContain("Texas"); }); }
// ENDSAMPLE // SAMPLE: assert-on-text public Task assert_on_content(ISystemUnderTest system) { return(system.Scenario(_ => { _.ContentShouldBe("exactly this"); _.ContentShouldContain("some snippet"); _.ContentShouldNotContain("some warning"); })); }
public Scenario(ISystemUnderTest system) { _system = system; Body = new HttpRequestBody(system, this); ConfigureHttpContext(c => { c.Request.Body = new MemoryStream(); c.Response.Body = new MemoryStream(); }); }
// ENDSAMPLE // SAMPLE: read-text public async Task read_text(ISystemUnderTest system) { var result = await system.Scenario(_ => { _.Get.Url("/output"); }); // This deserializes the response body to the // designated Output type var outputString = result.ResponseBody.ReadAsText(); // do assertions against the Output string }
// SAMPLE: check-the-status-code public Task check_the_status(ISystemUnderTest system) { return(system.Scenario(_ => { // Shorthand for saying that the StatusCode should be 200 _.StatusCodeShouldBeOk(); // Or a specific status code _.StatusCodeShouldBe(403); // Ignore the status code altogether _.IgnoreStatusCode(); })); }
public Scenario(ISystemUnderTest system) { _system = system; Body = new HttpRequestBody(system, this); ConfigureHttpContext(c => { c.Request.Body = new MemoryStream(); // For Core 3.0 TestServer, replacing the response stream causes issues. For earlier versions, // _not_ replacing it causes issues. #if !NETCOREAPP3_0 c.Response.Body = new MemoryStream(); #endif }); }
// ENDSAMPLE // SAMPLE: read-xml public async Task read_xml(ISystemUnderTest system) { var result = await system.Scenario(_ => { _.Get.Url("/output"); }); // This deserializes the response body to the // designated Output type var output = result.ResponseBody.ReadAsXml <Output>(); // do assertions against the Output model // OR, if you just want the XmlDocument itself: XmlDocument document = result.ResponseBody.ReadAsXml(); }
// SAMPLE: ScenarioSignature /// <summary> /// Define and execute an integration test by running an Http request through /// your ASP.Net Core system /// </summary> /// <param name="system"></param> /// <param name="configure"></param> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> public static async Task <IScenarioResult> Scenario( this ISystemUnderTest system, Action <Scenario> configure) // ENDSAMPLE { var scenario = new Scenario(system); configure(scenario); scenario.Rewind(); HttpContext context = null; try { context = await system.Invoke(c => { system.BeforeEach(c).GetAwaiter().GetResult(); c.Request.Body.Position = 0; scenario.SetupHttpContext(c); if (c.Request.Path == null) { throw new InvalidOperationException("This scenario has no defined url"); } }); scenario.RunAssertions(context); } finally { await system.AfterEach(context); } if (context.Response.Body.CanSeek) { context.Response.Body.Position = 0; } return(new ScenarioResult(context, system)); }
// ENDSAMPLE // SAMPLE: asserting-on-header-values public Task asserting_on_header_values(ISystemUnderTest system) { return(system.Scenario(_ => { // Assert that there is one and only one value equal to "150" _.Header("content-length").SingleValueShouldEqual("150"); // Assert that there is no value for this response header _.Header("connection").ShouldNotBeWritten(); // Only write one value for this header _.Header("set-cookie").ShouldHaveOneNonNullValue(); // Check the content-type header _.ContentTypeShouldBe("text/json"); })); }
// SAMPLE: conneg-helpers public Task conneg_helpers(ISystemUnderTest system) { return(system.Scenario(_ => { // Set the accepts header on the request _.Get.Url("/").Accepts("text/plain"); // Specify the etag header value _.Get.Url("/").Etag("12345"); // Set the content-type header on the request _.Post.Url("/").ContentType("text/json"); // This is a superset of the code above that // will set the content-type header as well _.Post.Json(new InputModel()).ToUrl("/"); })); }
// SAMPLE: write-form-data public Task write_form_data(ISystemUnderTest system) { var form1 = new Dictionary <string, string> { ["a"] = "what?", ["b"] = "now?", ["c"] = "really?" }; return(system.Scenario(_ => { // This writes the dictionary values to the HTTP // request as form data, and sets the content-length // header as well as setting the content-type // header to application/x-www-form-urlencoded _.Context.WriteFormData(form1); })); }
// ENDSAMPLE // SAMPLE: sending-xml public Task send_xml(ISystemUnderTest system) { return(system.Scenario(_ => { // This serializes the Input object to xml, // writes it to the HttpRequest.Body, and sets // the accepts & content-type header values to // application/xml _.Post .Xml(new Input { Name = "Max", Age = 13 }) .ToUrl("/person"); // OR, if url lookup is enabled, this is an equivalent: _.Post.Xml(new Input { Name = "Max", Age = 13 }); })); }
// ENDSAMPLE // SAMPLE: asserting-on-header-values public Task asserting_on_header_values(ISystemUnderTest system) { return(system.Scenario(_ => { // Assert that there is one and only one value equal to "150" _.Header("content-length").SingleValueShouldEqual("150"); // Assert that there is no value for this response header _.Header("connection").ShouldNotBeWritten(); // Only write one value for this header _.Header("set-cookie").ShouldHaveOneNonNullValue(); // Assert that the header has the given values _.Header("www-authenticate").ShouldHaveValues("NTLM", "Negotiate"); // Assert that the header matches a regular expression _.Header("location").SingleValueShouldMatch(new Regex(@"^/items/\d*$")); // Check the content-type header _.ContentTypeShouldBe("text/json"); })); }
public ResponseExpression(ISystemUnderTest system, Action <Scenario> configure) { _system = system; _configure = configure; }
/// <summary> /// Shortcut to just retrieve the contents of an HTTP GET as JSON and deserialize the resulting /// response to the type "T" /// </summary> /// <param name="system"></param> /// <param name="url"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static async Task <T> GetAsJson <T>(this ISystemUnderTest system, string url) { var response = await system.Scenario(x => x.Get.Url(url).Accepts("application/json;text/json")); return(response.ResponseBody.ReadAsJson <T>()); }
public HttpRequestBody(ISystemUnderTest system, Scenario parent) { _system = system; _parent = parent; }
public Scenario(ISystemUnderTest system, IServiceScope scope) { _system = system; Context = system.CreateContext(); Context.RequestServices = scope.ServiceProvider; }