public void SetUp()
        {
            _fakeHackerNewsServer.ResetMappings();

            _fixture.Register <IHackerNewsHtmlParser>(_fixture.Create <HackerNewsHtmlParser>);
            _fixture.Register <IHackerNewsPostValidator>(_fixture.Create <HackerNewsPostValidator>);

            var httpClient = new HttpClient
            {
                BaseAddress = new Uri($"http://localhost:{_fakeHackerNewsServer.Ports.First()}")
            };

            _fixture.Inject <IHttpClientService>(new HttpClientService(httpClient, null));

            _hackerNewsScraper = _fixture.Create <HackerNewsScraper>();
        }
        public void FluentMockServer_Should_reset_mappings()
        {
            // given
            _server = FluentMockServer.Start();

            _server
            .Given(Request.Create()
                   .WithPath("/foo")
                   .UsingGet())
            .RespondWith(Response.Create()
                         .WithBody(@"{ msg: ""Hello world!""}"));

            // when
            _server.ResetMappings();

            // then
            Check.That(_server.Mappings).IsEmpty();
            Check.ThatAsyncCode(() => new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo"))
            .ThrowsAny();
        }
Exemple #3
0
        private void SetupBankResponse(Guid id, bool wasSuccessful, string error)
        {
            _fakeBankServer.ResetMappings();

            var response = new
            {
                id,
                wasSuccessful,
                error
            };

            _fakeBankServer
            .Given(Request
                   .Create()
                   .WithPath("/payments/process")
                   .UsingPost())
            .RespondWith(Response
                         .Create()
                         .WithStatusCode(200)
                         .WithHeader("Content-Type", "application/json")
                         .WithBody(JsonConvert.SerializeObject(response)));
        }
        //
        private SemaphoreSlim SetupResponse(FluentMockServer server, IDictionary <string, string> data, UpdateMode mode)
        {
            var signal = new SemaphoreSlim(0, 1);

            server.ResetMappings();
            var resp = Response.Create().WithCallback(req =>
            {
                signal.Release();
                var respBuilder = mode.IsStreaming ?
                                  Response.Create().WithEventsBody(StreamingData(data)) :
                                  Response.Create().WithJsonBody(PollingData(data));
                return(((Response)respBuilder).ResponseMessage);
            });

            // Note: in streaming mode, since WireMock.Net doesn't seem to support streaming responses, the fake response will close
            // after the end of the data-- so the SDK will enter retry mode and we may get another identical streaming request. For
            // the purposes of these tests, that doesn't matter. The correct processing of a chunked stream is tested in the
            // LaunchDarkly.EventSource tests, and the retry logic is tested in LaunchDarkly.CommonSdk.

            server.Given(Request.Create().WithPath(path => Regex.IsMatch(path, mode.FlagsPathRegex)))
            .RespondWith(resp);
            return(signal);
        }
        public async Task FluentMockServer_Should_respond_to_valid_matchers_when_sent_json()
        {
            // Assign
            var validMatchersForHelloServerJsonMessage = new List <object[]>
            {
                new object[] { new WildcardMatcher("*Hello server*"), "application/json" },
                new object[] { new WildcardMatcher("*Hello server*"), "text/plain" },
                new object[] { new ExactMatcher(jsonRequestMessage), "application/json" },
                new object[] { new ExactMatcher(jsonRequestMessage), "text/plain" },
                new object[] { new RegexMatcher("Hello server"), "application/json" },
                new object[] { new RegexMatcher("Hello server"), "text/plain" },
                new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "application/json" },
                new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "text/plain" }
            };

            _server = FluentMockServer.Start();

            foreach (var item in validMatchersForHelloServerJsonMessage)
            {
                string path = $"/foo_{Guid.NewGuid()}";
                _server
                .Given(Request.Create().WithPath(path).WithBody((IMatcher)item[0]))
                .RespondWith(Response.Create().WithBody("Hello client"));

                // Act
                var content  = new StringContent(jsonRequestMessage, Encoding.UTF8, (string)item[1]);
                var response = await new HttpClient().PostAsync("http://localhost:" + _server.Ports[0] + path, content);

                // Assert
                var responseString = await response.Content.ReadAsStringAsync();

                Check.That(responseString).Equals("Hello client");

                _server.ResetMappings();
                _server.ResetLogEntries();
            }
        }