Got the forwarding http content properly tests working, way to much going on in the requester

This commit is contained in:
TomPallister 2016-09-19 16:26:44 +01:00
parent f3128cffe0
commit 741fcc644d
5 changed files with 49 additions and 40 deletions

View File

@ -1,5 +1,6 @@
using System.IO; using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using Flurl; using Flurl;
using Flurl.Http; using Flurl.Http;
@ -15,17 +16,30 @@ namespace Ocelot.Library.Infrastructure.Requester
Stream content, Stream content,
IHeaderDictionary headers, IHeaderDictionary headers,
IRequestCookieCollection cookies, IRequestCookieCollection cookies,
IQueryCollection queryString) IQueryCollection queryString,
string contentType)
{ {
var method = new HttpMethod(httpMethod); var method = new HttpMethod(httpMethod);
var streamContent = new StreamContent(content); var streamContent = new StreamContent(content);
if (!string.IsNullOrEmpty(contentType))
{
var splitCt = contentType.Split(';');
var cT = splitCt[0];
streamContent.Headers.ContentType = new MediaTypeHeaderValue(cT);
}
if (headers != null)
{
headers.Remove("Content-Type");
}
if (content.Length > 0) if (content.Length > 0)
{ {
return await downstreamUrl return await downstreamUrl
.SetQueryParams(queryString) .SetQueryParams(queryString)
.WithCookies(cookies) .WithCookies(cookies)
.WithHeaders(streamContent.Headers) .WithHeaders(headers)
.SendAsync(method, streamContent); .SendAsync(method, streamContent);
} }

View File

@ -14,6 +14,7 @@ namespace Ocelot.Library.Infrastructure.Requester
Stream content, Stream content,
IHeaderDictionary headers, IHeaderDictionary headers,
IRequestCookieCollection cookies, IRequestCookieCollection cookies,
IQueryCollection queryString); IQueryCollection queryString,
string contentType);
} }
} }

View File

@ -49,7 +49,7 @@ namespace Ocelot.Library.Middleware
var response = await _requester var response = await _requester
.GetResponse(context.Request.Method, downstreamUrl, context.Request.Body, .GetResponse(context.Request.Method, downstreamUrl, context.Request.Body,
context.Request.Headers, context.Request.Cookies, context.Request.Query); context.Request.Headers, context.Request.Cookies, context.Request.Query, context.Request.ContentType);
await _responder.CreateResponse(context, response); await _responder.CreateResponse(context, response);

View File

@ -1,19 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Ocelot.AcceptanceTests.Fake;
using Ocelot.Library.Infrastructure.Configuration;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
using YamlDotNet.Serialization;
namespace Ocelot.AcceptanceTests namespace Ocelot.AcceptanceTests
{ {
using System;
using System.Net.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Xunit;
using Ocelot.AcceptanceTests.Fake;
using Shouldly;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Library.Infrastructure.Configuration;
using TestStack.BDDfy;
using YamlDotNet.Serialization;
public class OcelotTests : IDisposable public class OcelotTests : IDisposable
{ {
private readonly FakeService _fakeService; private readonly FakeService _fakeService;
@ -21,7 +21,7 @@ namespace Ocelot.AcceptanceTests
private HttpClient _client; private HttpClient _client;
private HttpResponseMessage _response; private HttpResponseMessage _response;
private readonly string _configurationPath; private readonly string _configurationPath;
private string _postContent; private StringContent _postContent;
public OcelotTests() public OcelotTests()
{ {
@ -82,10 +82,9 @@ namespace Ocelot.AcceptanceTests
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.Created)) .Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.Created))
.BDDfy(); .BDDfy();
} }
private void GivenThePostHasContent(string postcontent) private void GivenThePostHasContent(string postcontent)
{ {
_postContent = postcontent; _postContent = new StringContent(postcontent);
} }
/// <summary> /// <summary>
@ -126,8 +125,7 @@ namespace Ocelot.AcceptanceTests
private void WhenIPostUrlOnTheApiGateway(string url) private void WhenIPostUrlOnTheApiGateway(string url)
{ {
var content = new StringContent(_postContent); _response = _client.PostAsync(url, _postContent).Result;
_response = _client.PostAsync(url, content).Result;
} }
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode) private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)

View File

@ -27,6 +27,7 @@ namespace Ocelot.UnitTests.Requester
private IHeaderDictionary _headers; private IHeaderDictionary _headers;
private IRequestCookieCollection _cookies; private IRequestCookieCollection _cookies;
private IQueryCollection _query; private IQueryCollection _query;
private string _contentType;
public RequesterTests() public RequesterTests()
{ {
@ -65,6 +66,7 @@ namespace Ocelot.UnitTests.Requester
this.Given(x => x.GivenIHaveHttpMethod("POST")) this.Given(x => x.GivenIHaveHttpMethod("POST"))
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk")) .And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
.And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom"))) .And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom")))
.And(x => x.GivenTheContentTypeIs("application/json"))
.And(x => x.GivenTheDownstreamServerReturns(HttpStatusCode.Created)) .And(x => x.GivenTheDownstreamServerReturns(HttpStatusCode.Created))
.When(x => x.WhenIMakeARequest()) .When(x => x.WhenIMakeARequest())
.Then(x => x.ThenTheFollowingIsReturned(HttpStatusCode.Created)) .Then(x => x.ThenTheFollowingIsReturned(HttpStatusCode.Created))
@ -79,29 +81,18 @@ namespace Ocelot.UnitTests.Requester
{ {
this.Given(x => x.GivenIHaveHttpMethod("POST")) this.Given(x => x.GivenIHaveHttpMethod("POST"))
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk")) .And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
.And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom") .And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom")))
{ .And(x => x.GivenTheContentTypeIs("application/json"))
Headers =
{
{"Boom", "TickTick"}
}
}))
.And(x => x.GivenTheDownstreamServerReturns(HttpStatusCode.Created)) .And(x => x.GivenTheDownstreamServerReturns(HttpStatusCode.Created))
.When(x => x.WhenIMakeARequest()) .When(x => x.WhenIMakeARequest())
.Then(x => x.ThenTheFollowingIsReturned(HttpStatusCode.Created)) .Then(x => x.ThenTheFollowingIsReturned(HttpStatusCode.Created))
.And(x => x.ThenTheDownstreamServerIsCalledCorrectly()) .And(x => x.ThenTheDownstreamServerIsCalledCorrectly())
.And(x => x.ThenTheCorrectHttpMethodIsUsed(HttpMethod.Post)) .And(x => x.ThenTheCorrectHttpMethodIsUsed(HttpMethod.Post))
.And(x => x.ThenTheCorrectContentIsUsed(new StringContent("Hi from Tom") .And(x => x.ThenTheCorrectContentIsUsed(new StringContent("Hi from Tom")))
{
Headers =
{
{ "Boom", "TickTick" }
}
}))
.And(x => x.ThenTheCorrectContentHeadersAreUsed(new HeaderDictionary .And(x => x.ThenTheCorrectContentHeadersAreUsed(new HeaderDictionary
{ {
{ {
"Boom", "TickTick" "Content-Type", "application/json"
} }
})) }))
.BDDfy(); .BDDfy();
@ -165,6 +156,11 @@ namespace Ocelot.UnitTests.Requester
.BDDfy(); .BDDfy();
} }
private void GivenTheContentTypeIs(string contentType)
{
_contentType = contentType;
}
private void ThenTheCorrectQueryStringIsUsed(string expected) private void ThenTheCorrectQueryStringIsUsed(string expected)
{ {
_httpTest.CallLog[0].Request.RequestUri.Query.ShouldBe(expected); _httpTest.CallLog[0].Request.RequestUri.Query.ShouldBe(expected);
@ -211,7 +207,7 @@ namespace Ocelot.UnitTests.Requester
foreach (var expectedHeader in expectedHeaders) foreach (var expectedHeader in expectedHeaders)
{ {
_httpTest.CallLog[0].Request.Content.Headers.ShouldContain(x => x.Key == expectedHeader.Key _httpTest.CallLog[0].Request.Content.Headers.ShouldContain(x => x.Key == expectedHeader.Key
//&& x.Value.First() == expectedHeader.Value[0] && x.Value.First() == expectedHeader.Value[0]
); );
} }
} }
@ -246,7 +242,7 @@ namespace Ocelot.UnitTests.Requester
_result = _httpRequester _result = _httpRequester
.GetResponse(_httpMethod, _downstreamUrl, .GetResponse(_httpMethod, _downstreamUrl,
_content != null ? _content.ReadAsStreamAsync().Result : Stream.Null, _content != null ? _content.ReadAsStreamAsync().Result : Stream.Null,
_headers, _cookies, _query).Result; _headers, _cookies, _query, _contentType).Result;
} }
private void ThenTheFollowingIsReturned(HttpStatusCode expected) private void ThenTheFollowingIsReturned(HttpStatusCode expected)