Example #1
0
        /// <summary>
        /// Sends the HTTP request asynchronous.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>A task representing the HTTP response.</returns>
        /// <exception cref="InvalidOperationException">The IWebServer implementation should be TestWebServer.</exception>
        public async Task <TestHttpResponse> SendAsync(TestHttpRequest request)
        {
            var context = new TestHttpContext(request, WebServer);

            if (!(WebServer is TestWebServer testServer))
            {
                throw new InvalidOperationException($"The {nameof(IWebServer)} implementation should be {nameof(TestWebServer)}.");
            }

            testServer.HttpContexts.Enqueue(context);

            if (!(context.Response is TestHttpResponse response))
            {
                throw new InvalidOperationException("The response object is invalid.");
            }

            try
            {
                while (!response.IsClosed)
                {
                    await Task.Delay(1);
                }
            }
            catch
            {
                // ignore
            }

            return(response);
        }
Example #2
0
            public async Task ValidWebApiPathInvalidMethod_Returns405()
            {
                var request = new TestHttpRequest(WebServerUrl + TestRegexController.RelativePath + "regex/1", HttpVerbs.Delete);

                var response = await SendAsync(request);

                Assert.AreEqual((int)HttpStatusCode.MethodNotAllowed, response.StatusCode);
            }
Example #3
0
        public async Task RequestWithNoAuthorizationHeader_ReturnsUnauthorized()
        {
            var request = new TestHttpRequest(WebServerUrl);

            using (var response = await SendAsync(request))
            {
                Assert.AreEqual((int)HttpStatusCode.Unauthorized, response.StatusCode, "Status Code Unauthorized");
            }
        }
Example #4
0
        public async Task RequestOptionsVerb_ReturnsOK()
        {
            var request = new TestHttpRequest(WebServerUrl + TestController.GetPath, HttpVerbs.Options);

            request.Headers.Add(HttpHeaders.Origin, "http://unosquare.github.io");
            request.Headers.Add(HttpHeaders.AccessControlRequestMethod, "post");
            request.Headers.Add(HttpHeaders.AccessControlRequestHeaders, "content-type");

            using (var response = await SendAsync(request))
            {
                Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode, "Status Code OK");
            }
        }
Example #5
0
        public async Task <TestHttpResponse> SendAsync(TestHttpRequest request)
        {
            if (WebServerInstance is TestWebServer testWebServer)
            {
                return(await testWebServer.GetClient().SendAsync(request));
            }

            using (var client = new HttpClient())
            {
                var response = await client.SendAsync(request.ToHttpRequestMessage());

                return(response.ToTestHttpResponse());
            }
        }
Example #6
0
        public async Task RequestWithInvalidCredentials_ReturnsUnauthorized()
        {
            var request   = new TestHttpRequest(WebServerUrl);
            var byteArray = Encoding.ASCII.GetBytes("root:password1233");
            var authData  = new System.Net.Http.Headers.AuthenticationHeaderValue("basic",
                                                                                  Convert.ToBase64String(byteArray));

            request.Headers.Add("Authorization", authData.ToString());

            using (var response = await SendAsync(request))
            {
                Assert.AreEqual((int)HttpStatusCode.Unauthorized, response.StatusCode, "Status Code Unauthorized");
            }
        }
Example #7
0
 public static HttpRequestMessage ToHttpRequestMessage(this TestHttpRequest request)
 {
     return(new HttpRequestMessage());
 }