Skip to content

Commit

Permalink
Merge pull request #36 from mattolenik/master
Browse files Browse the repository at this point in the history
Add ability to set request content dynamically
  • Loading branch information
hibri committed Apr 14, 2016
2 parents 021b1eb + 1644c07 commit 1a38372
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ public void If_no_Mocked_Endpoints_matched_then_should_return_404_with_HttpMockE
}
}

[Test]
public void Should_return_dynamic_data()
{
string value = "test1";
_httpMockRepository
.Stub(x => x.Get("/endpoint"))
.Return(() => value)
.OK();
AssertResponse("GET", "test1");
value = "test2";
AssertResponse("GET", "test2");
}

private void AssertResponse(string method, string expected)
{
var webRequest = (HttpWebRequest) WebRequest.Create(_endpointToHit);
Expand Down
19 changes: 9 additions & 10 deletions src/HttpMock/BufferedBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ namespace HttpMock
{
class BufferedBody : IResponse
{
ArraySegment<byte> data;
readonly Func<byte[]> dataFunc;

public BufferedBody(string data) : this(data, Encoding.UTF8) { }
public BufferedBody(string data, Encoding encoding) : this(encoding.GetBytes(data)) { }
public BufferedBody(byte[] data) : this(new ArraySegment<byte>(data)) { }
public BufferedBody(ArraySegment<byte> data)
public BufferedBody(byte[] data) : this(() => data) { }
public BufferedBody(Func<string> data) : this(() => Encoding.UTF8.GetBytes(data())) { }
public BufferedBody(Func<byte[]> data)
{
this.data = data;
Length = data.Count;
this.dataFunc = data;
}

public int Length { get; private set; }
public Func<int> Length => () => dataFunc().Length;

public IDisposable Connect(IDataConsumer channel)
public IDisposable Connect(IDataConsumer channel)
{
// null continuation, consumer must swallow the data immediately.
channel.OnData(data, null);
var bytes = new ArraySegment<byte>(dataFunc());
channel.OnData(bytes, null);
channel.OnEnd();
return null;
}

public void SetRequestHeaders(IDictionary<string, string> requestHeaders) {

}
}

Expand All @@ -40,7 +40,6 @@ public IDisposable Connect(IDataConsumer channel) {
}

public void SetRequestHeaders(IDictionary<string, string> requestHeaders) {

}
}
}
1 change: 1 addition & 0 deletions src/HttpMock/IRequestStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace HttpMock
public interface IRequestStub
{
IRequestStub Return(string responseBody);
IRequestStub Return(Func<string> responseBody);
IRequestStub ReturnFile(string pathToFile);
IRequestStub WithParams(IDictionary<string, string> nameValueCollection);
void OK();
Expand Down
31 changes: 18 additions & 13 deletions src/HttpMock/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

namespace HttpMock
{
public class RequestHandler : IRequestHandler, IRequestStub, IRequestVerify
{
public class RequestHandler : IRequestHandler, IRequestStub, IRequestVerify
{
private readonly ResponseBuilder _webResponseBuilder = new ResponseBuilder();
private readonly IList<Func<string, bool>> _constraints = new List<Func<string, bool>>();
private readonly IList<Func<string, bool>> _constraints = new List<Func<string, bool>>();
private readonly Queue<ReceivedRequest> _observedRequests = new Queue<ReceivedRequest>();

public RequestHandler(string path, IRequestProcessor requestProcessor) {
Expand All @@ -34,16 +34,21 @@ public IRequestStub Return(string responseBody) {
return this;
}

public IRequestStub Return(Func<string> responseBody) {
_webResponseBuilder.Return(responseBody);
return this;
}

public IRequestStub ReturnFile(string pathToFile) {
_webResponseBuilder.WithFile(pathToFile);

return this;
}

public IRequestStub ReturnFileRange(string pathToFile, int from, int to)
{
_webResponseBuilder.WithFileRange(pathToFile, from, to);

return this;
}

Expand Down Expand Up @@ -80,10 +85,10 @@ public IRequestStub AddHeader(string header, string headerValue) {
}

public IRequestStub WithUrlConstraint(Func<string, bool> constraint)
{
_constraints.Add(constraint);
return this;
}
{
_constraints.Add(constraint);
return this;
}

public override string ToString() {
var sb = new StringBuilder();
Expand All @@ -107,10 +112,10 @@ public string GetBody() {
return _observedRequests.Peek().Body;
}

public bool CanVerifyConstraintsFor(string url)
{
return _constraints.All(c => c(url));
}
public bool CanVerifyConstraintsFor(string url)
{
return _constraints.All(c => c(url));
}

public ReceivedRequest LastRequest()
{
Expand Down
28 changes: 17 additions & 11 deletions src/HttpMock/ResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ public class ResponseBuilder
private HttpStatusCode _httpStatusCode = HttpStatusCode.OK;
private string _contentType = "text/plain";
private IResponse _response = new BufferedBody("");
private int _contentLength = 0;
private Func<int> _contentLength = () => 0;
private Dictionary<string, string> _headers = new Dictionary<string, string>();

public ResponseBuilder Return(string body) {
var bufferedBody = new BufferedBody(body);
_contentLength = bufferedBody.Length;
_response = bufferedBody;

var bufferedBody = new BufferedBody(body);
_contentLength = bufferedBody.Length;
_response = bufferedBody;

return this;
}

public ResponseBuilder Return(Func<string> body) {
var bufferedBody = new BufferedBody(body);
_contentLength = bufferedBody.Length;
_response = bufferedBody;

return this;
}

Expand All @@ -30,17 +38,15 @@ public IDataProducer BuildBody(IDictionary<string, string> headers) {
}

public HttpResponseHead BuildHeaders() {
AddHeader(HttpHeaderNames.ContentType, _contentType);
AddHeader(HttpHeaderNames.ContentLength, _contentLength.ToString());
AddHeader(HttpHeaderNames.ContentType, _contentType);
AddHeader(HttpHeaderNames.ContentLength, _contentLength().ToString());

var headers = new HttpResponseHead
{
Status = string.Format("{0} {1}", (int)_httpStatusCode, _httpStatusCode),
Headers = _headers
};
return headers;


}

public void WithStatus(HttpStatusCode httpStatusCode) {
Expand All @@ -54,7 +60,7 @@ public void WithContentType(string contentType) {
public void WithFile(string pathToFile) {
if(File.Exists(pathToFile)) {
var fileInfo = new FileInfo(pathToFile);
_contentLength = (int)fileInfo.Length;
_contentLength = () => (int)fileInfo.Length;
_response = new FileResponseBody(pathToFile);
} else {
throw new InvalidOperationException("File does not exist/accessible at :" + pathToFile);
Expand All @@ -66,7 +72,7 @@ public void WithFileRange(string pathToFile, int from, int to)
if (File.Exists(pathToFile))
{
var fileInfo = new FileInfo(pathToFile);
_contentLength = (to - from) +1;
_contentLength = () => (to - from) + 1;
_response = new FileResponseBody(pathToFile);
AddHeader(HttpResponseHeader.ContentRange.ToString(), string.Format("bytes={0}-{1}/{2}", from, to, fileInfo.Length));
}
Expand Down

0 comments on commit 1a38372

Please sign in to comment.