Skip to content

Commit

Permalink
Use fresh content streams for successive requests
Browse files Browse the repository at this point in the history
Fixes #32.

Implementations of `IWebMockResponseBodySource` now return new streams
on calling `GetContentStream`. The content streams are freed by the Indy
HTTP server after completion.
  • Loading branch information
rhatherall committed Oct 15, 2020
1 parent 8acd3ab commit 44ea5c2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Source/Delphi.WebMock.ResponseContentFile.pas
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ constructor TWebMockResponseContentFile.Create(const AFileName: string; const AC
end;

function TWebMockResponseContentFile.GetContentStream: TStream;
var
LContentStream: TMemoryStream;
begin
Result := FContentStream;
LContentStream := TMemoryStream.Create;
LContentStream.CopyFrom(FContentStream, 0);
Result := LContentStream;
end;

function TWebMockResponseContentFile.GetContentType: string;
Expand Down
6 changes: 1 addition & 5 deletions Source/Delphi.WebMock.ResponseContentString.pas
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ interface
TWebMockResponseContentString = class(TInterfacedObject,
IWebMockResponseBodySource)
private
FContentStream: TStream;
FContentString: string;
FContentType: string;
procedure SetContentType(const Value: string);
Expand Down Expand Up @@ -72,10 +71,7 @@ procedure TWebMockResponseContentString.SetContentType(const Value: string);

function TWebMockResponseContentString.GetContentStream: TStream;
begin
if not Assigned(FContentStream) then
FContentStream := TStringStream.Create(ContentString);

Result := FContentStream;
Result := TStringStream.Create(ContentString);
end;

function TWebMockResponseContentString.GetContentString: string;
Expand Down
38 changes: 38 additions & 0 deletions Tests/Features/Delphi.WebMock.ResponsesWithBody.Tests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ TWebMockResponsesWithBodyTests = class(TObject)
[Test]
procedure Response_WhenWithBodyIsString_ReturnsUTF8CharSet;
[Test]
procedure Response_WhenWithBodyIsStringOnMultipleCalls_Succeeds;
[Test]
procedure Response_WhenWithBodyFileWithoutContentType_SetsContentTypeToInferedType;
[Test]
procedure Response_WhenWithBodyFileWithContentType_SetsContentType;
[Test]
procedure Response_WhenWithBodyFileOnMultipleCalls_Succeeds;
end;

implementation
Expand All @@ -67,6 +71,23 @@ implementation
System.Net.HttpClient,
TestHelpers;

procedure TWebMockResponsesWithBodyTests.Response_WhenWithBodyFileOnMultipleCalls_Succeeds;
var
LExpected: string;
LResponse: IHTTPResponse;
begin
LExpected := '{ "key": "value" }'#10;
WebMock.StubRequest('GET', '/json').ToRespond.WithBodyFile(FixturePath('Response.json'));

// First request
LResponse := WebClient.Get(WebMock.URLFor('json'));
Assert.AreEqual(LExpected, LResponse.ContentAsString);

// Second request
LResponse := WebClient.Get(WebMock.URLFor('json'));
Assert.AreEqual(LExpected, LResponse.ContentAsString);
end;

procedure TWebMockResponsesWithBodyTests.Response_WhenWithBodyFileWithContentType_SetsContentType;
var
LExpected: string;
Expand Down Expand Up @@ -128,6 +149,23 @@ procedure TWebMockResponsesWithBodyTests.Response_WhenWithBodyIsAString_ReturnsS
Assert.AreEqual(LExpectedContent, LContentText);
end;

procedure TWebMockResponsesWithBodyTests.Response_WhenWithBodyIsStringOnMultipleCalls_Succeeds;
var
LContent: string;
LResponse: IHTTPResponse;
begin
LContent := 'Some text...';
WebMock.StubRequest('GET', '/text').ToRespond.WithBody(LContent);

// First request
LResponse := WebClient.Get(WebMock.URLFor('text'));
Assert.EndsWith(LContent, LResponse.ContentAsString);

// Second request
LResponse := WebClient.Get(WebMock.URLFor('text'));
Assert.EndsWith(LContent, LResponse.ContentAsString);
end;

procedure TWebMockResponsesWithBodyTests.Response_WhenWithBodyIsString_ReturnsUTF8CharSet;
var
LResponse: IHTTPResponse;
Expand Down
4 changes: 1 addition & 3 deletions Tests/Fixtures/Response.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"key": "value"
}
{ "key": "value" }

0 comments on commit 44ea5c2

Please # to comment.