Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Fluent response API #10

Merged
merged 6 commits into from
May 27, 2022
Merged

feat: Add Fluent response API #10

merged 6 commits into from
May 27, 2022

Conversation

skwasjer
Copy link
Owner

@skwasjer skwasjer commented May 26, 2022

Per the discussion in #9 this PR adds a new Fluent response API, replacing the current overload based API.

mock.When(_ => {})
    .Respond(with => with
        .StatusCode(200)
        .Body("my text", Encoding.UTF8)
        .ContentType("text/plain")
    );

JSON

mock.When(_ => {})
    .Respond(with => with
        .StatusCode(HttpStatusCode.BadRequest)
        .JsonBody(new { error = "oops" })
    );

Additionally, it adds capabilities to simulate network latency:

mock.When(_ => {})
    .Respond(with => with
        ...
        .Latency(ThreeG)
    );

And network transfer rates (only with streams):

Stream stream = ...; // A big stream

mock.When(_ => {})
    .Respond(with => with
        .Body(new RateLimitedStream(stream, 512000)) // Rate limited to 512 kbps
    );

Furthermore, .TimesOut and TimesOutAfter alternatives have been renamed to .ClientTimeout(). A .ServerTimeout() was also added which instead of throwing a TaskCancelledException after a specified amount of time returns a response with a HTTP 408 Request Timeout instead.

There is still work to be done, notably ensuring all 'old' API's are deprecated. I think I did most but probably there are some gaps left.

One notable issue is the inconsistency with the request API, eg. .Content() and .JsonContent() whereas this new response API uses .Body() and .JsonBody() respectively (still doubting whether or not to just go with .Json(). So probably have to align those too. The downside is more of the API surface changes, making refactoring/adoption harder.

Alpha packages (from this PR) can be fetched from this repo its NuGet feed for testing (it is not published to NuGet until merged):
https://ci.appveyor.com/nuget/mockhttp/packages

@skwasjer skwasjer changed the title Add Fluent response API feat: Add Fluent response API May 26, 2022
@skwasjer skwasjer added the enhancement New feature or request label May 26, 2022
@skwasjer skwasjer force-pushed the feature/fluent_response_api branch from 0c70251 to 341ef96 Compare May 27, 2022 00:36
@codecov-commenter
Copy link

Codecov Report

Merging #10 (0c70251) into main (58b2183) will decrease coverage by 1.10%.
The diff coverage is 82.00%.

❗ Current head 0c70251 differs from pull request most recent head 341ef96. Consider uploading reports for the commit 341ef96 to get more accurate results

Impacted Files Coverage Δ
...son/Extensions/MockHttpRequestContextExtensions.cs 100.00% <ø> (ø)
src/MockHttp.Json/Newtonsoft/RespondsExtensions.cs 50.00% <ø> (ø)
src/MockHttp.Json/RespondsExtensions.cs 50.00% <25.00%> (-23.92%) ⬇️
src/MockHttp/Responses/HttpContentBehavior.cs 66.66% <66.66%> (ø)
...c/MockHttp/Extensions/ResponseBuilderExtensions.cs 68.91% <68.91%> (ø)
src/MockHttp/Extensions/IRespondsExtensions.cs 84.93% <73.33%> (-10.31%) ⬇️
src/MockHttp/Responses/NetworkLatencyBehavior.cs 75.00% <75.00%> (ø)
src/MockHttp/Extensions/ListExtensions.cs 78.94% <78.94%> (ø)
src/MockHttp/NetworkLatency.cs 83.33% <83.33%> (ø)
...MockHttp/Language/Flow/Response/ResponseBuilder.cs 85.18% <85.18%> (ø)
... and 12 more

@skwasjer skwasjer merged commit 9724f5b into main May 27, 2022
@skwasjer skwasjer deleted the feature/fluent_response_api branch May 27, 2022 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants