public async Task MockHttpClientExceptionIsThrownIfMultipleUnmatchesOccur()
        {
            var expected =
                "\n\nSetup:\nSetupGet(\"/api\")\n\nDid not match the HTTP method for request:\nPOST /api\nAccept: application/json\r\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n"
                + "-------\n\nSetup:\nSetupPost(\"api\")\n\nDid not match the Uri for request:\nPOST /api\nAccept: application/json\r\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n"
                + "-------\n\nSetup:\nSetupPost(\"/api\", headers: x => (Convert(x.Accept, String) == \"wrong accept header\"))\n\nDid not match the headers for request:\nPOST /api\nAccept: application/json\r\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n"
                + "-------\n\nSetup:\nSetupPost<System.String>(\"/api\", content: x => (x == \"wrong content body\"))\n\nDid not match the content for request:\nPOST /api\nAccept: application/json\r\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n";

            var mock = new MockHttpClient();

            mock.SetupGet("/api").ReturnsAsync(200);
            mock.SetupPost("api").ReturnsAsync(201);
            mock.SetupPost("/api", headers: x => x.Accept == "wrong accept header").ReturnsAsync(201);
            mock.SetupPost <string>("/api", content: x => x == "wrong content body").ReturnsAsync(201);

            var request = new SystemHttpRequestMessage(HttpMethod.Post, "/api");

            request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = new System.Net.Http.StringContent("actual body");

            var exception = await Assert.ThrowsAsync <MockHttpClientException>(() => mock.Object.SendAsync(request));

            Assert.True(exception.Reason.HasFlag(ExceptionReasonTypes.UnmatchedHttpMethod));
            Assert.True(exception.Reason.HasFlag(ExceptionReasonTypes.UnmatchedRequestUri));
            Assert.True(exception.Reason.HasFlag(ExceptionReasonTypes.UnmatchedHeaders));
            Assert.True(exception.Reason.HasFlag(ExceptionReasonTypes.UnmatchedContent));
            Assert.Equal(expected, exception.Message);
        }
Beispiel #2
0
        public async Task CreateAsync_should_create_and_return_new_todo()
        {
            var todo     = new Todo(0, 456, "go shopping");
            var expected = new Todo(2, 456, "go shopping");

            var responseHeaders = new HttpResponseHeaders();

            responseHeaders.Add("Location", $"https://localhost/todos/{expected.Id}");

            // pass lambda predicate to validate request content
            // response from the HTTP Post method will have 201 status code and Location header
            mock.SetupPost <Todo>("/todos",
                                  content: x => x.UserId == todo.UserId &&
                                  x.Title == todo.Title &&
                                  x.Completed == todo.Completed).ReturnsAsync(201, responseHeaders);

            mock.SetupGet($"/todos/{expected.Id}").ReturnsAsync(expected);

            var actual = await service.CreateAsync(todo);

            Assert.Equal(expected.Id, actual.Id);
            Assert.Equal(expected.UserId, actual.UserId);
            Assert.Equal(expected.Title, actual.Title);
            Assert.Equal(expected.Completed, actual.Completed);
        }
Beispiel #3
0
        public async Task TestRequestMessageWithIs()
        {
            var mock = new MockHttpClient();

            var request = new SystemHttpRequestMessage(HttpMethod.Post, "/");

            request.Content = new System.Net.Http.StringContent("test");
            request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            mock.SetupPost <StringContent>("/", headers: x => x.Accept == Is.Equal("application/json"), content: x => x == Is.Any <StringContent>()).ReturnsAsync(201);

            var result = await mock.Object.SendAsync(request);

            Assert.Equal(201, (int)result.StatusCode);
        }
        public async Task MockHttpClientExceptionIsThrownIfUnmatchedContent()
        {
            var expected = "\n\nSetup:\nSetupPost<System.String>(\"/api\", content: x => (x == \"wrong content body\"))\n\nDid not match the content for request:\nPOST /api\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n";

            var mock = new MockHttpClient();

            mock.SetupPost <string>("/api", content: x => x == "wrong content body").ReturnsAsync(200);

            var request = new SystemHttpRequestMessage(HttpMethod.Post, "/api");

            request.Content = new System.Net.Http.StringContent("actual body");

            var exception = await Assert.ThrowsAsync <MockHttpClientException>(() => mock.Object.SendAsync(request));

            Assert.Equal(ExceptionReasonTypes.UnmatchedContent, exception.Reason);
            Assert.Equal(expected, exception.Message);
        }
Beispiel #5
0
        public async Task TestContentHeadersWithStringType()
        {
            var mock = new MockHttpClient();

            var request = new SystemHttpRequestMessage(HttpMethod.Post, "/");

            request.Content = new System.Net.Http.StringContent("test", System.Text.Encoding.ASCII, "application/json");
            request.Content.Headers.Allow.Add("GET");
            request.Content.Headers.Allow.Add("POST");
            request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            mock.SetupPost <string>("/", headers: x => x.Accept == "application/json" && x.ContentType == "application/json; charset=us-ascii", content: x => x == "test").ReturnsAsync(201);

            var result = await mock.Object.SendAsync(request);

            Assert.Equal(201, (int)result.StatusCode);
        }
Beispiel #6
0
        public async Task TestRequestMessageWithObject()
        {
            var mock = new MockHttpClient();

            var employee = new Employee()
            {
                Id   = 1,
                Name = "John Smith"
            };

            mock.SetupPost <Employee>("/employees", content: x => x.Name == Is.NotNull <string>()).ReturnsAsync(201);

            var content = new System.Net.Http.StringContent(Utils.Json.ToString(employee), System.Text.Encoding.ASCII, "application/json");

            var result = await mock.Object.PostAsync("/employees", content);

            Assert.Equal(201, (int)result.StatusCode);
        }
        public async Task MockHttpClientExceptionIsThrownIfUnmatchedResult()
        {
            var expected = "\n\nUnmatched setup result:\nStatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StringContent, Headers:\r\n{\r\n  Content-Type: text/plain; charset=utf-8\r\n}\n";

            var mock    = new MockHttpClient();
            var content = new StringContent("content");

            mock.SetupPost("/api").ReturnsAsync(200).ReturnsAsync(404, content);

            var request = new SystemHttpRequestMessage(HttpMethod.Post, "/api");

            request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = new System.Net.Http.StringContent("actual body");

            var result = await mock.Object.SendAsync(request);

            var exception = Assert.Throws <MockHttpClientException>(() => mock.VerifyAll());

            Assert.Equal(ExceptionReasonTypes.UnmatchedResult, exception.Reason);
            Assert.Equal(expected, exception.Message);
        }