Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.VisualStudio.TestPlatform.Common;
- using Newtonsoft.Json;
- using NUnit.Framework;
- using System.Net;
- using System.Text;
- [Binding]
- public class FirstFeatureSteps
- {
- private readonly ScenarioContext _context;
- private HttpResponseMessage _response;
- public FirstFeatureSteps(ScenarioContext context)
- {
- _context = context;
- }
- [Given(@"I have generated request data for a new user with name ""(.*)"" and email ""(.*)""")]
- public void GivenIhaveGeneratedRequestDataForANewUserWithNameAndEmail(string name, string email)
- {
- var requestData = new UserBuilder()
- .WithName(name)
- .WithEmail(email)
- .BuildAsJson();
- _context.Set(requestData, "requestData");
- }
- [Given(@"I have generated request data for a new user with name ""(.*)"" but without an email")]
- public void GivenIHaveGeneratedRequestDataForAnewUserWithNameButWithoutAnEmail(string name)
- {
- var requestData = new UserBuilder()
- .WithName(name)
- .WithoutEmail()
- .BuildAsJson();
- _context.Set(requestData, "requestData");
- }
- [When(@"I submit the request to the user registration endpoint")]
- public async Task WhenISubmitTheRequestToTheUserRegistrationEndpoint()
- {
- var client = new HttpClient();
- var requestData = _context.Get<string>("requestData");
- _response = await client.PostAsync("http://localhost:5184/api/user",
- new StringContent(requestData, Encoding.UTF8, "application/json"));
- _context.Set(_response, "apiResponse");
- //check response
- if (!_response.IsSuccessStatusCode)
- {
- Console.WriteLine($"API call failed with status code: {_response.StatusCode}");
- return;
- }
- }
- [Then(@"The response should be a success")]
- public async Task ThenTheReponseShouldBeASuccess()
- {
- var response = _context.Get<HttpResponseMessage>("apiResponse");
- //assert response has status 200
- Assert.IsTrue(response.IsSuccessStatusCode, $"Expected a success response but got {response.StatusCode}.");
- //read response content as a string
- var responseContent = await response.Content.ReadAsStringAsync();
- //validate content against Json schema
- var schemaFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Schemas", "FirstFeature.json");
- bool isValid = await SchemaValidator.IsValidJsonPayloadAgainstSchema(responseContent, schemaFilePath);
- //assert response matches schema
- Assert.IsTrue(isValid, "The response does not match the schema");
- }
- [Then(@"The response should indicate a failure")]
- public async Task ThenTheResponseShouldIndicateAFailure()
- {
- var requestData = _context.Get<string>("requestData");
- var response = _context.Get<HttpResponseMessage>("apiResponse");
- var responseContent = await response.Content.ReadAsStringAsync();
- if(string.IsNullOrWhiteSpace(responseContent))
- {
- throw new InvalidOperationException("The response content is empty or null.");
- }
- Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode, "Expected a BadRequest response.");
- dynamic errorResponse = JsonConvert.DeserializeObject<dynamic>(responseContent);
- var errorMessage = (string)errorResponse.errors.email[0];
- Assert.AreEqual("The Email field is required.", errorMessage, "Expected error message is not present");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement