fake_server v0.4.1 FakeServer
The server itself.
Summary
Functions
Modifies behavior of a running server
Just an alias to run/3 with empty options
This function starts a new server. You must provide:
- a
name
to the server: This identifies the server, and is used to shut it down. - a
status_list
: This is an array containing the statuses of the server. Each request to the server will receive as response the first status on this list. The status is then removed from the list. If no more statuses are available, the server will respondHTTP 200
. - some
options
. Currently the only option accepted isport
Stops a running server. You must provide the server name
Functions
Modifies behavior of a running server.
Return values
If the server is running, it will return :ok
. Otherwise, it will return {:error, :not_found}
.
Also, if the status list contains one or more inexistent status an :invalid_status error will be returned.
Examples
FakeServer.modify_behavior(:running_server, [:new_status1, :new_status2])
:ok
FakeServer.modify_behavior(:invalid_server, [:new_status])
{:error, :server_not_found}
FakeServer.modify_behavior(:invalid_server, [:inexistent_status])
{:error, {:invalid_status, :inexistent_status}}
FakeServer.modify_behavior(:invalid_server, [])
{:error, :no_status}
This function starts a new server. You must provide:
- a
name
to the server: This identifies the server, and is used to shut it down. - a
status_list
: This is an array containing the statuses of the server. Each request to the server will receive as response the first status on this list. The status is then removed from the list. If no more statuses are available, the server will respondHTTP 200
. - some
options
. Currently the only option accepted isport
.
Return values
If the server is started without any error, the return will be {:ok, address}
tuple. The address
is the ip:port
the server listen on.
Currently 127.0.0.1
is the single ip
value. The port is a random number between 5001
and 10000
.
Options
You can set some options on a map. Currently, the only option accepted is port
. Take a look at the examples to learn how it works.
Examples
## create some status
FakeServer.Status.create(:status200, %{response_code: 200, response_body: ~s<"username": "mr_user">})
FakeServer.Status.create(:status500, %{response_code: 500, response_body: ~s<"error": "internal server error">})
FakeServer.Status.create(:status403, %{response_code: 403, response_body: ~s<"error": "forbidden">})
FakeServer.run(:server1, :status200) ## with only one status, [] are not needed
{:ok, "127.0.0.1:9925"}
FakeServer.run(:server2, [:status200, :status500]) ## with a list of statuses
{:ok, "127.0.0.1:8388"}
FakeServer.run(:server3, [:status500, :status403], %{port: 5000}) ## with a default port
{:ok, "127.0.0.1:5000"}
FakeServer.run(:server3, [:status200, :status200]) ## you can repeat same status multiple times
{:ok, "127.0.0.1:7293"}