NetworkBehavior class¶
Defined in
Namespace: Refit.Testing
Assembly: Refit.Testing.dll
Full name: Refit.Testing.NetworkBehavior
Modifiers: public sealed
Summary¶
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¶
| Name | Summary |
|---|---|
| .ctor | Initializes a new instance of the [NetworkBehavior](# class with seed 0. |
Properties¶
| Name | Summary |
|---|---|
| Delay | Gets or sets the base delay applied to each request. Defaults to 2 seconds. |
| Variance | Gets or sets the latency variance as a fraction of [Delay](# (0 = none). Defaults to 0.4. |
| FailurePercent | Gets or sets the probability (0..1) that a request simulates a network failure. Defaults to 0.03. |
| ErrorPercent | Gets or sets the probability (0..1) that a request simulates an HTTP error response. Defaults to 0. |
| ErrorStatusCode | Gets or sets the status code returned for a simulated HTTP error. Defaults to 500. |
| FailureFactory | Gets or sets the factory that produces the exception thrown for a simulated network failure. |
Methods¶
| Name | Summary |
|---|---|
| NextDelay | Calculates the next delay, applying the configured variance around [Delay](# |
| NextIsFailure | Determines whether the next request should simulate a network failure. |
| NextIsError | Determines whether the next request should simulate an HTTP error response. |
| CreateFailure | Creates the exception used to simulate a network failure. |
| CreateErrorResponse | Creates the response used to simulate an HTTP error. |