Exemple #1
0
        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 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);
        }
Exemple #3
0
        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);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        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);
        }
Exemple #8
0
        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);
        }