WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.42k stars 209 forks source link

Question : Do we have provision to read the Response data from a file? #115

Closed raghavendrabankapur closed 6 years ago

raghavendrabankapur commented 6 years ago

@StefH We have a huge data that need to be responded with and also this is been used by other mocks. Can we respond to a matching request with a file content?

StefH commented 6 years ago

Hello @raghavendrabankapur

This is not yet described in the Wiki (maybe I need to restructure the wiki), but you can use

BodyAsFile in the JSON mapping or in C# code:

/// <summary>
/// WithBodyFromFile : Create a ... response based on a File.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="cache">Defines if this file is cached in memory or retrieved from disk everytime the response is created.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithBodyFromFile([NotNull] string filename, bool cache = true);
raghavendrabankapur commented 6 years ago

@StefH in that case, the file name should be the full path of the file , or file should be placed somewhere the similar way of __admin/mappings.

StefH commented 6 years ago

As of now, you need to provide the full path. Maybe it's a good idea that the code can support both.

/// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/>
public IResponseBuilder WithBodyFromFile(string filename, bool cache = true)
{
    Check.NotNull(filename, nameof(filename));

    ResponseMessage.BodyEncoding = null;
    ResponseMessage.BodyAsFileIsCached = cache;

    if (cache)
    {
        ResponseMessage.Body = null;
        ResponseMessage.BodyAsBytes = File.ReadAllBytes(filename);
        ResponseMessage.BodyAsFile = null;
    }
    else
    {
        ResponseMessage.Body = null;
        ResponseMessage.BodyAsBytes = null;
        ResponseMessage.BodyAsFile = filename;
    }

    return this;
}
StefH commented 6 years ago

Also this linked issue could help you ? https://github.com/WireMock-Net/WireMock.Net/issues/173

Closing this issue for now. If you still have question, please reopen or create a new issue.