Пример #1
0
        public void WriteRequest_should_append_headers_after_host()
        {
            // Arrange
            var stringBuilder = new StringBuilder();
            var logWriter = GetHttpLogWriter(stringBuilder);
            var headers = new List<HeaderItem>()
            {
                new HeaderItem("Cookie", "aaa=bbb;loggedin=true"),
                new HeaderItem("Accept-Language", "en-US"),
                new HeaderItem("Accept", "text/html")
            };

            var requestDetails = new RequestDetails()
            {
                Method = "post",
                Url = "http://en.wikipedia.org/wiki/Microsoft?a=b",
                Headers = headers
            };

            // Act
            logWriter.AppendRequest(requestDetails);

            // Assert
            string[] lines = stringBuilder.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            Assert.AreEqual("POST http://en.wikipedia.org/wiki/Microsoft?a=b HTTP/1.1", lines[0]);
            Assert.AreEqual("Host: en.wikipedia.org", lines[1]);
            Assert.AreEqual("Cookie: aaa=bbb;loggedin=true", lines[2]);
            Assert.AreEqual("Accept-Language: en-US", lines[3]);
            Assert.AreEqual("Accept: text/html", lines[4]);
            Assert.AreEqual("", lines[5]);
        }
Пример #2
0
        public virtual void AppendRequest(RequestDetails requestDetails)
        {
            if (string.IsNullOrEmpty(requestDetails.Method))
                return;

            if (string.IsNullOrEmpty(requestDetails.Url))
                return;

            if (!Uri.IsWellFormedUriString(requestDetails.Url, UriKind.Absolute))
                return;

            Uri uri = new Uri(requestDetails.Url);
            Writer.WriteLine(REQUEST_LINE_FORMAT, requestDetails.Method.ToUpper(), requestDetails.Url);
            Writer.WriteLine(HEADER_FORMAT, "Host", uri.Host);

            if (requestDetails.Headers != null)
            {
                foreach (var keyValuePair in requestDetails.Headers)
                {
                    Writer.WriteLine(HEADER_FORMAT, keyValuePair.Key, keyValuePair.Value);
                }
            }

            if (!string.IsNullOrEmpty(requestDetails.Body))
                Writer.WriteLine(requestDetails.Body);

            Writer.WriteLine();
        }
Пример #3
0
        public void WriteRequest_should_ignore_invalid_url_parameter()
        {
            // Arrange
            var stringBuilder = new StringBuilder();
            var logWriter = GetHttpLogWriter(stringBuilder);
            var requestDetails = new RequestDetails()
            {
                Method = null,
                Url = "not a valid url",
                Headers = new List<HeaderItem>()
            };

            // Act
            logWriter.AppendRequest(requestDetails);

            // Assert
            Assert.AreEqual("", stringBuilder.ToString());
        }
Пример #4
0
        public void WriteRequest_should_allow_null_for_headers()
        {
            // Arrange
            var stringBuilder = new StringBuilder();
            var logWriter = GetHttpLogWriter(stringBuilder);
            var requestDetails = new RequestDetails()
            {
                Method = null,
                Url = "http://www.uri",
                Headers = null
            };

            // Act
            logWriter.AppendRequest(requestDetails);

            // Assert
            Assert.AreEqual("", stringBuilder.ToString());
        }
Пример #5
0
 public void AppendRequest(RequestDetails requestDetails)
 {
     RequestDetails = requestDetails;
 }
Пример #6
0
        public void WriteRequest_should_write_request_line_and_host_and_extra_newline_at_end()
        {
            // Arrange
            var stringBuilder = new StringBuilder();
            var logWriter = GetHttpLogWriter(stringBuilder);
            var requestDetails = new RequestDetails()
            {
                Method = "post",
                Url = "http://en.wikipedia.org/wiki/Microsoft?a=b",
                Headers = new List<HeaderItem>()
            };

            // Act
            logWriter.AppendRequest(requestDetails);

            // Assert
            string[] lines = stringBuilder.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            Assert.AreEqual("POST http://en.wikipedia.org/wiki/Microsoft?a=b HTTP/1.1", lines[0]);
            Assert.AreEqual("Host: en.wikipedia.org", lines[1]);
            Assert.AreEqual("", lines[2]);
        }
Пример #7
0
        public HttpResponse ExecuteRequest(string httpMethod, string url, string contentType, string postBody, IEnumerable<HeaderItem> headers)
        {
            Uri uri;
            if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
                throw new ArgumentException(url + " is not a valid Uri", "url");

            _restClient.BaseUrl = uri;
            _restClient.CookieContainer = _cookieContainer;

            //
            // Make the request adding the content-type, body and headers
            //
            Method method = GetMethodEnum(httpMethod);
            var request = new RestRequest(method);
            if (method == Method.POST)
            {
                // From the RestSharp docs:
                // "The name of the parameter will be used as the Content-Type header for the request."
                request.AddParameter(contentType, postBody, ParameterType.RequestBody);
            }

            if (headers != null)
            {
                headers = headers.ToList();
                foreach (var keyValuePair in headers)
                {
                    request.AddHeader(keyValuePair.Key, keyValuePair.Value);
                }
            }

            _lastRequest = new RequestDetails()
            {
                Body = postBody,
                Headers = headers,
                Method = httpMethod,
                Url = url
            };

            //
            // Get the response back, parsing the headers
            //
            DateTime startTime = DateTime.UtcNow;
            IRestResponse response = _restClient.Execute(request);
            TimeSpan responseTime = DateTime.UtcNow - startTime;

            List<KeyValuePair<string, string>> keyvaluePairs = new List<KeyValuePair<string, string>>();
            if (response.Headers != null)
            {
                keyvaluePairs = response.Headers.Select(x => new KeyValuePair<string, string>(x.Name, Convert.ToString(x.Value)))
                                                .ToList();
            }

            _lastResponse = new ResponseDetails()
            {
                BodyResponse = response.Content,
                Headers = keyvaluePairs,
                Status = response.StatusCode
            };

            return new HttpResponse()
            {
                StatusCode = response.StatusCode,
                Content = response.Content,
                Headers = keyvaluePairs,
                ResponseTime = responseTime
            };
        }