Skip to content

NetworkBehavior class

Defined in

Namespace: Refit.Testing Assembly: Refit.Testing.dll Full name: Refit.Testing.NetworkBehavior Modifiers: public sealed

Summary

View source

Deterministic network-condition simulation for StubHttp, modelled on Retrofit's NetworkBehavior. Attach one to a handler to inject latency, latency variance, simulated network failures (a thrown HttpRequestException) and simulated HTTP errors (a non-2xx response), so retry, timeout and ApiException handling can be exercised under test.

Applies to

net10.0, net10.0-browserwasm1.0, net10.0-desktop1.0, net9.0, net9.0-browserwasm1.0, net9.0-desktop1.0, net8.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.2, net8.0-macos14.5, net8.0-tvos17.2, netstandard2.1, net462, net481, net471

Remarks

All randomness is driven by a seeded Random, so a given seed reproduces the same sequence of delays and faults within a runtime. Defaults mirror Retrofit: a 2 second delay, 40% variance, 3% failure and 0% error. The calculation methods are usable standalone.

Constructors

NameSummary
.ctorInitializes a new instance of the [NetworkBehavior](# class with seed 0.

Properties

NameSummary
DelayGets or sets the base delay applied to each request. Defaults to 2 seconds.
VarianceGets or sets the latency variance as a fraction of [Delay](# (0 = none). Defaults to 0.4.
FailurePercentGets or sets the probability (0..1) that a request simulates a network failure. Defaults to 0.03.
ErrorPercentGets or sets the probability (0..1) that a request simulates an HTTP error response. Defaults to 0.
ErrorStatusCodeGets or sets the status code returned for a simulated HTTP error. Defaults to 500.
FailureFactoryGets or sets the factory that produces the exception thrown for a simulated network failure.

Methods

NameSummary
NextDelayCalculates the next delay, applying the configured variance around [Delay](#
NextIsFailureDetermines whether the next request should simulate a network failure.
NextIsErrorDetermines whether the next request should simulate an HTTP error response.
CreateFailureCreates the exception used to simulate a network failure.
CreateErrorResponseCreates the response used to simulate an HTTP error.
Inherited members