Class ApiStub
Represents an Api stub that responds to certain requests with preconfigured responses.
Inheritance
Implements
Inherited Members
Namespace: Stubbery
Assembly: Stubbery.dll
Syntax
public class ApiStub : IDisposable
Remarks
After instantiating and setting up the stubbed endpoints, the stub needs to be started by calling the Start() method.
Constructors
| Improve this Doc View SourceApiStub()
Creates a new instance of ApiStub
Declaration
public ApiStub()
Remarks
The constructor does not start up the stub web server. In order to start listening for requests, the Start() method needs to be called.
Properties
| Improve this Doc View SourceAddress
Gets the full url at which the stub is accessible.
Declaration
public string Address { get; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
When calling the Start() method, the stub is started on a randomly picked free port. Thus Address will return a url like "http://localhost:51234".
Port
Sets the Port for the stub to start />
Declaration
public int? Port { set; }
Property Value
Type | Description |
---|---|
System.Nullable<System.Int32> |
Remarks
The Post property cannot be set after the stub has already started. The Start() method needs to be called after the Port is set.
Methods
| Improve this Doc View SourceDelete()
Sets up a new stubbed DELETE request.
Declaration
public ISetup Delete()
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Delete(String, CreateStubResponse)
Sets up a new stubbed DELETE request on a specific route with a response.
Declaration
public ISetup Delete(string route, CreateStubResponse responder)
Parameters
Type | Name | Description |
---|---|---|
System.String | route | The route on which the stub will respond. |
CreateStubResponse | responder | The stubbed response to send. |
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Dispose()
Stops the stub server.
Declaration
public void Dispose()
Remarks
The stub needs to be disposed when it's not needed, so the TCP port used is freed up.
EnsureStarted()
Ensures that the stub server is started.
Declaration
public void EnsureStarted()
Remarks
The stub is started on a randomly picked free port, so it will be accessible on an url like "http://localhost:51234". The exact address can be retrieved through the Address property. If the stub has already been started, then the method returns without doing anything.
Get()
Sets up a new stubbed GET request.
Declaration
public ISetup Get()
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Get(String, CreateStubResponse)
Sets up a new stubbed GET request on a specific route with a response.
Declaration
public ISetup Get(string route, CreateStubResponse responder)
Parameters
Type | Name | Description |
---|---|---|
System.String | route | The route on which the stub will respond. |
CreateStubResponse | responder | The stubbed response to send. |
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Post()
Sets up a new stubbed POST request.
Declaration
public ISetup Post()
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Post(String, CreateStubResponse)
Sets up a new stubbed POST request on a specific route with a response.
Declaration
public ISetup Post(string route, CreateStubResponse responder)
Parameters
Type | Name | Description |
---|---|---|
System.String | route | The route on which the stub will respond. |
CreateStubResponse | responder | The stubbed response to send. |
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Put()
Sets up a new stubbed PUT request.
Declaration
public ISetup Put()
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Put(String, CreateStubResponse)
Sets up a new stubbed PUT request on a specific route with a response.
Declaration
public ISetup Put(string route, CreateStubResponse responder)
Parameters
Type | Name | Description |
---|---|---|
System.String | route | The route on which the stub will respond. |
CreateStubResponse | responder | The stubbed response to send. |
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Request(HttpMethod[])
Sets up a new stubbed request for the specified HTTP methods
.
Declaration
public ISetup Request(params HttpMethod[] methods)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod[] | methods | The HTTP methods for which we want to respond. |
Returns
Type | Description |
---|---|
ISetup | An ISetup object on which further conditions can be set. |
Start()
Starts the stub server and starts listening to requests.
Declaration
public void Start()
Remarks
The stub is started on a randomly picked free port, so it will be accessible on an url like "http://localhost:51234". The exact address can be retrieved through the Address property.
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | The stub has already been started. |