All Projects → bernardolins → Fake_server

bernardolins / Fake_server

Licence: apache-2.0
FakeServer integrates with ExUnit to make external APIs testing simpler

Programming Languages

elixir
2628 projects
macros
77 projects

Projects that are alternatives of or similar to Fake server

stub-server
Stub server for REST APIs
Stars: ✭ 14 (-78.12%)
Mutual labels:  test, stub, fake
Impersonator
Ruby library to record and replay object interactions
Stars: ✭ 100 (+56.25%)
Mutual labels:  fake, stub
ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (+60.94%)
Mutual labels:  stub, fake
Faker.Portable
C# faked data generation for testing and prototyping purpose.
Stars: ✭ 12 (-81.25%)
Mutual labels:  stub, fake
jaymock-cli
Mock an API and generate fake JSON test data, right from the terminal.
Stars: ✭ 13 (-79.69%)
Mutual labels:  test, fake
Clj Fakes
An isolation framework for Clojure/ClojureScript.
Stars: ✭ 26 (-59.37%)
Mutual labels:  fake, stub
node-mock-examples
Examples of tests that mock Node system APIs: fs, http, child_process, timers
Stars: ✭ 38 (-40.62%)
Mutual labels:  test, stub
Stubmatic
Mock HTTP calls without coding. Designed specially for testing and testers.
Stars: ✭ 118 (+84.38%)
Mutual labels:  stub, fake
Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (+70.31%)
Mutual labels:  test, stub
Strictly fake
Stub that automatically verifies that stubbed methods exist and the signatures match the original.
Stars: ✭ 18 (-71.87%)
Mutual labels:  fake, stub
Jaymock
Minimal fake JSON test data generator.
Stars: ✭ 28 (-56.25%)
Mutual labels:  fake, test
Evilgrade
Evilgrade is a modular framework that allows the user to take advantage of poor upgrade implementations by injecting fake updates.
Stars: ✭ 1,086 (+1596.88%)
Mutual labels:  fake
Testify
A unit testing framework written in bash for bash scripts
Stars: ✭ 45 (-29.69%)
Mutual labels:  test
Ezxss
ezXSS is an easy way for penetration testers and bug bounty hunters to test (blind) Cross Site Scripting.
Stars: ✭ 1,022 (+1496.88%)
Mutual labels:  test
Telegram Test Api
Simple implimentation of telegram API which can be used for testing telegram bots
Stars: ✭ 42 (-34.37%)
Mutual labels:  test
Test demo
Testing Using Python Demo. 使用Python测试脚本demo。
Stars: ✭ 60 (-6.25%)
Mutual labels:  test
Backbone Faux Server
A framework for mocking up server-side persistence / processing for Backbone.js
Stars: ✭ 55 (-14.06%)
Mutual labels:  test
Orleanstestkit
Unit Test Toolkit for Microsoft Orleans
Stars: ✭ 42 (-34.37%)
Mutual labels:  test
Should Enzyme
Useful functions for testing React Components with Enzyme.
Stars: ✭ 41 (-35.94%)
Mutual labels:  test
Metalperformanceshadersproxy
A proxy for MetalPerformanceShaders which takes to a stub on a simulator and to the real implementation on iOS devices.
Stars: ✭ 41 (-35.94%)
Mutual labels:  stub

FakeServer

Build Status Coverage Status Hex.pm

FakeServer is an HTTP server that simulates responses. It can be used in test and development environments, helping to validate the behavior of your application if there are errors or unexpected responses from some external HTTP service.

It provides the test_with_server macro to be used together with ExUnit, making it easier to write tests that need to request external services. Instead of creating a mock when you need make a request, you can use a real HTTP server that will reply a deterministic response. This way you can validate if your application can handle it.

FakeServer can also be used through functions, when ExUnit is not available (in the console, for example).

Installation

Important: From the version 2.0, FakeServer only supports cowboy 2.x. If you have cowboy 1.x as dependency, use FakeServer version 1.5.

FakeServer is available on Hex. First, add it to mix.exs as a test dependency:

def deps do
  [
    {:fake_server, "~> 2.1", only: :test}
  ]
end

Then, start fake_server application on test/test_helper.exs.

{:ok, _} = Application.ensure_all_started(:fake_server)

Basic Usage

For more examples you can see the docs.

ExUnit

FakeServer provides the macro FakeServer.test_with_server. It works like ExUnit's test macro, but before your test starts it will run an HTTP server in a random port (by default). The server will be available until test case is finished.

You can use the FakeServer.route macro to add a route and setup it's response. Use FakeServer.http_address to get the address of the server running in the current test. Each test will start its own HTTP server.

defmodule MyTest do
  use ExUnit.Case
  import FakeServer

  test_with_server "returns 404 if a request is made to a non-configured route" do
    response = HTTPoison.get!("#{FakeServer.address}/not/configured")
    assert response.status_code == 404
  end

  test_with_server "when the response is a structure it returns the given response" do
    route "/test", Response.no_content!()
    response = HTTPoison.get!("#{FakeServer.address}/test")
    assert response.status_code == 204
  end

  test_with_server "when the response is a list it returns the first element of the list and removes it" do
    route "/test", [Response.ok!(), Response.no_content!()]
    response = HTTPoison.get!("#{FakeServer.address}/test")
    assert response.status_code == 200
    response = HTTPoison.get!("#{FakeServer.address}/test")
    assert response.status_code == 204
  end

  test_with_server "when the response is a function it runs the function" do
    route "/say/hi", fn(_) -> IO.puts "HI!" end
    response = HTTPoison.get! "#{FakeServer.address}/say/hi"
  end

  test_with_server "computes hits for the corresponding route" do
    route "/test", Response.no_content!()
    assert hits() == 0
    assert hits("/test") == 0
    HTTPoison.get!("#{FakeServer.address}/test")
    assert hits() == 1
    assert hits("/test") == 1
  end

  test_with_server "supports inline port configuration", [port: 55_000] do
    assert FakeServer.port() == 55_000
  end
end

Setup Server

If you need to do some setup before every test_with_server tests, you can define a setup_test_with_server/1 function in your module. This function will receive a %FakeServer.Instance{} struct as a parameter.

Standalone Server

You can use a fake server without ExUnit with FakeServer.start and other helper functions available. Functions work similar to macros, but can be used outside the tests.

iex> {:ok, pid} = FakeServer.start(:my_server)
{:ok, #PID<0.302.0>}

iex> :ok = FakeServer.put_route(pid, "/say/hi", fn(_) -> IO.puts "HI!" end)
:ok

iex> {:ok, port} = FakeServer.port(:my_server)
{:ok, 62698}

For more examples you can see the docs.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].