public async Task Should_Verify_Expecation_Was_Met_On_MockServer() { // Act await Client.SetupAsync(exp => exp.OnHandling(HttpMethod.Get, req => req.WithPath("/test")) .RespondOnce(201, resp => resp.WithDelay(50, TimeUnit.Milliseconds)) .Setup()); var request = new HttpRequestMessage(HttpMethod.Get, new Uri("test", UriKind.Relative)); var response = await Client.SendAsync(request); response.EnsureSuccessStatusCode(); // Act var builder = new FluentHttpRequestBuilder(); builder.WithMethod(HttpMethod.Get).WithPath("test"); // Act var verification = Verify.Once(builder.Build()); response = await Client.VerifyAsync(verification); // Assert response.StatusCode.Should().Be(HttpStatusCode.Accepted); }
public void SerializeJObject_Should_Use_SerializeJObject_Of_Properties() { // Arrange var builder = new FluentHttpRequestBuilder() .WithPath("") .WithMethod(HttpMethod.Post) .WithContent(body => body.WithoutXmlContent("<xml here>")); var expectation = FluentExpectationBuilder.Create( httpRequest: builder.Build(), httpResponse: HttpResponse.Create(headers: new Dictionary <string, string[]> { { "Content-Type", new[] { "xml" } } }, body: new JValue("some xml response"), delay: new Delay(TimeUnit.Milliseconds, 50), statusCode: 200) ); // Act var jo = expectation.AsJObject(); var json = Serializer.Serialize(expectation); Logger.LogInformation("JSON", json); // Assert jo["httpResponse"]["body"].ToObject <string>().Should().Be("some xml response"); }
public IFluentVerificationBuilder.IWithRequest Verify([NotNull] Action <IFluentHttpRequestBuilder> request) { var builder = new FluentHttpRequestBuilder(); request(builder); _httpRequest = builder.Build(); return(this); }
public IWithRequest OnHandling([CanBeNull] HttpMethod method = null, Action <IFluentHttpRequestBuilder> requestFactory = null) { var builder = new FluentHttpRequestBuilder(); requestFactory?.Invoke(builder); builder.WithMethod(method); HttpRequest = builder.Build(); return(this); }
public IWithRequest OnHandlingAny(HttpMethod method = null) { if (method is null) { HttpRequest = null; return(this); } HttpRequest = new FluentHttpRequestBuilder().WithMethod(method).Build(); return(this); }
public void Should_Set_Path(string inputPath, string expectedPath) { // Arrange var builder = new FluentHttpRequestBuilder(); // Act var result = builder.WithPath(inputPath).Build().AsJson(); Logger.LogResult("JSON", result); // Assert result.Should().MatchRegex($@"(?ms){{.*""path"":.*""{expectedPath}"".*}}.*"); }
public void Should_Keep_Alive() { // Arrange var builder = new FluentHttpRequestBuilder(); // Act var result = builder.KeepConnectionAlive().Build().AsJson(); Logger.LogResult("JSON", result); // Assert result.Should().MatchRegex(@"(?ms){.*""keepAlive"": true.*}.*"); }
public void Should_Use_Encryption() { // Arrange var builder = new FluentHttpRequestBuilder(); // Act var result = builder.EnableEncryption().Build().AsJson(); Logger.LogResult("JSON", result); // Assert result.Should().MatchRegex(@"(?ms){.*""secure"": true.*}.*"); }
public void Should_Set_Content_Type_Header() { // Arrange var builder = new FluentHttpRequestBuilder(); // Act var result = builder .AddContentType(CommonContentType.Soap12) .Build(); Logger.LogResult("JSON ", result.AsJson()); // Assert // TODO:reactive }
public void Should_Serialize_Xml() { var request = new FluentHttpRequestBuilder(new FluentExpectationBuilder()) .WithXml("<bookstore> <book nationality=\"ITALIAN\" category=\"COOKING\"><title lang=\"en\">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book> </bookstore>"); var serialized = JsonConvert.SerializeObject(request.Body); var dict = new Dictionary <string, object>(); dict.Add("type", "XML"); dict.Add("xml", "<bookstore> <book nationality=\"ITALIAN\" category=\"COOKING\"><title lang=\"en\">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book> </bookstore>"); var expected = JsonConvert.SerializeObject(dict); _logger.WriteLine(expected); serialized.Should().Be(expected); }
public void Should_Use_ExpectationConverter_When_Using_Standard_Serializer() { // Arrange var expected = JObject.Parse(@"{ ""httpRequest"": { ""path"": ""/some/path"" } }").ToString(Formatting.Indented); var builder = new FluentHttpRequestBuilder() .WithPath("/some/path"); var expectation = FluentExpectationBuilder.Create(httpRequest: builder.Build()); // Act var json = JsonConvert.SerializeObject(expectation); // Assert json.Should().Be(expected); }
public void Should_Convert_To_Expectation_When_Converting_From_String() { // Arrange const string json = @"{ ""httpRequest"": { ""path"": ""/some/path"" }, ""httpResponse"": { ""statusCode"": 201, ""delay"": { ""timeUnit"": ""MILLISECONDS"", ""value"": 1 } }, ""times"": { ""remainingTimes"": 1, ""unlimited"": false } }"; var builder = new FluentHttpRequestBuilder() .WithPath("/some/path"); var expected = FluentExpectationBuilder.Create( httpRequest: builder.Build(), httpResponse: HttpResponse.Create( statusCode: 201, delay: new Delay(TimeUnit.Milliseconds, 1)), times: Times.Once ); // Act var jsonReader = new JsonTextReader(new StringReader(json)); var sut = new ExpectationConverter(); var result = sut.ReadJson(jsonReader, typeof(Expectation), null, JsonSerializer.Create(Serializer.SerializerSettings)) as Expectation; // Assert result .Should() .BeOfType <Expectation>() .Which .HttpRequest.Path.Should().Be(expected.HttpRequest.Path); }
public async void FluentHttpRequest_Send_ShouldReturnContent() { var mockHttp = new MockHttpMessageHandler(); mockHttp.When("https://sketch7.com/api/heroes/azmodan") .Respond("application/json", "{ 'name': 'Azmodan' }"); var fluentHttpClientFactory = GetNewClientFactory(); var clientBuilder = fluentHttpClientFactory.CreateBuilder("sketch7") .WithBaseUrl("https://sketch7.com") .WithMessageHandler(mockHttp); var httpClient = fluentHttpClientFactory.Add(clientBuilder); var fluentHttpRequest = new FluentHttpRequestBuilder(httpClient).WithUri("api/heroes/azmodan").Build(); var hero = await fluentHttpRequest.SendAsync <Hero>(); Assert.NotNull(hero); Assert.Equal("Azmodan", hero.Name); }
public void Should_Serialize_Json(MatchType?matchType) { var request = new FluentHttpRequestBuilder(new FluentExpectationBuilder()) .WithJson("{\"Hallo\": \"Test\"}", matchType); var serialized = JsonConvert.SerializeObject(request.Body, Formatting.Indented); var dict = new Dictionary <string, object>(); dict.Add("type", "JSON"); dict.Add("json", "{\"Hallo\": \"Test\"}"); if (matchType.HasValue) { dict.Add("matchType", matchType.Value.ToString().ToUpper()); } var expected = JsonConvert.SerializeObject(dict, Formatting.Indented); _logger.WriteLine(expected); serialized.Should().Be(expected); }
public void Should_Convert_Expectation_To_Json() { // Arrange var expected = JObject.Parse(@"{ ""httpRequest"": { ""path"": ""/some/path"" } }").ToString(Formatting.Indented); var builder = new FluentHttpRequestBuilder() .WithPath("/some/path"); var expectation = FluentExpectationBuilder.Create(httpRequest: builder.Build()); var subject = CreateSubject(out var sb, out var writer); // Act subject.WriteJson(writer, expectation, JsonSerializer.CreateDefault()); // Assert var json = sb.ToString(); json.Should().Be(expected); }
public void Should_Use_ExpectationConverter_When_Using_Standard_Deserializer() { // Arrange const string json = @"{ ""httpRequest"": { ""path"": ""/some/path"" } }"; var builder = new FluentHttpRequestBuilder().WithPath("/some/path"); // Act var unused = Verify.Once(builder.Build()); var expected = FluentExpectationBuilder.Create(httpRequest: builder.Build()); // Act var result = JsonConvert.DeserializeObject <Expectation>(json); // Assert result .Should() .BeOfType <Expectation>() .Which .HttpRequest.Path.Should().Be(expected.HttpRequest.Path); }