• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fake JSON response server

apidemic Build Status

Apidemic is a service for generating fake JSON response. You first register the sample JSON response, and apidemic will serve that response with random fake data.

This is experimental, so take it with a grain of salt.

Motivation

I got bored with hardcoding the sample json api response in tests. If you know golang, you can benefit by using the library, I have included a router that you can use to run disposable servers in your tests.

Installation

You can download the binaries for your respective operating system Download apidemic

Then put the downloaded binary somewhere in your system path.

Alternatively, if you have golang installed

go get github.com/gernest/apidemic/cmd/apidemic

Now you can start the service like this

apidemic start

This will run a service at localhost default port is 3000, you can change the port by adding a flag --port=YOUR_PORT_NUMBER

How to use

Lets say you expect a response like this

{
  "name": "anton",
  "age": 29,
  "nothing": null,
  "true": true,
  "false": false,
  "list": [
      "first",
      "second"
    ],
  "list2": [
    {
      "street": "Street 42",
      "city": "Stockholm"
      },
    {
      "street": "Street 42",
      "city": "Stockholm"
      }
    ],
  "address": {
    "street": "Street 42",
    "city": "Stockholm"
  },
  "country": {
    "name": "Sweden"
  }
}

If you have already started apidemic server you can register that response by making a POST request to the /register path. Passing the json body of the form.

{
  "endpoint": "test",
  "payload": {
    "name: first_name": "anton",
    "age: digits_n,max=2": 29,
    "nothing:": null,
    "true": true,
    "false": false,
    "list:word,max=3": [
      "first",
      "second"
    ],
    "list2": [
      {
        "street:street": "Street 42",
        "city:city": "Stockholm"
      },
      {
        "street": "Street 42",
        "city": "Stockholm"
      }
    ],
    "address": {
      "street:street": "Street 42",
      "city:city": "Stockholm"
    },
    "country": {
      "name:country": "Sweden"
    }
  }
}

See the annotation tags on the payload. Example if I want to generate full name for a field name I will just add "name:full_name".

Once your POST request is submitted you are good to ask for the response with fake values. Just make a GET request to the endpoint you registered.

So every GET call to /api/test will return the api response with fake data.

Routes

Apidemic server has only three http routes

/

This is the home path. It only renders information about the apidemic server.

/register

This is where you register endpoints. You POST the annotated sample JSON here. The request body should be a json object of signature.

{
	"endpoint":"my_endpoint",
	"payload": { ANNOTATED__SAMPLE_JSON_GOES_HERE },
}

/api/{REGISTERED_ENDPOINT_GOES_HERE}

Every GET request on this route will render a fake JSON object for the sample registered in this endpoint.

Other HTTP Methods

In case you need to mimic endpoints which respond to requests other than GET then make sure to add an http_method key with the required method name into your API description.

{
  "endpoint": "test",
  "http_method": "POST",
  "payload": {
    "name: first_name": "anton"
  }
}

Currently supported HTTP methods are: OPTIONS, GET, POST, PUT, DELETE, HEAD, default is GET. Please open an issue if you think there should be others added.

Emulate unexpected responses

Sometimes you need to ensure that your application handles API errors correctly in which case you can add a response_code_probabilities field with a map of response codes to probabilities.

{
  "endpoint": "test",
  "response_code_probabilities": {
    "404": 10,
    "503": 5,
    "418": 1
  },
  "payload": {
    "name: first_name": "anton"
  }
}

With the above configuration there's a 84% chance to get a 200 OK response. The server will respond with 404 Not Found about 1 out of 10 times and with 503 Service Unavailable 1 out of 20 times. There's also a 1% chance for the server to claim to be a Teapot.

Note: JSON keys must be strings, providing your response codes as integers will not work!

Tags

Apidemic uses tags to annotate what kind of fake data to generate and also control different requrements of fake data.

You add tags to object keys. For instance let's say you have a JSON object { "user_name": "gernest"}. If you want to have a fake username then you can annotate the key by adding user_name tag like this { "user_name:user_name": "gernest"}.

So JSON keys can be annotated by adding the : symbol then followed by comma separated list of tags. The first entry after : is for the tag type, the following entries are in the form key=value which will be the extra information to fine-tune your fake data. Please see the example above to see how tags are used.

Apidemic comes shipped with a large number of tags, meaning it is capable to generate a wide range of fake information.

These are currently available tags to generate different fake data:

Tag Details( data generated)
brand brand
character character
characters characters
characters_n characters of maximum length n
city city
color color
company company
continent continent
country country
credit_card_num credit card number
currency currency
currency_code currency code
day day
digits digits
digits_n digits of maximum number n
domain_name domain name
domain_zone domain zone
email_address email address
email_body email body
female_first_name female first name
female_full_name female full name
female_full_name_with_prefix female full name with prefix
female_full_name_with_suffix female full name with suffix
female_last_name female last name
female_last_name_pratronymic female last name pratronymic
first_name first name
full_name full name
full_name_with_prefix full name with prefix
full_name_with_suffix full name with suffix
gender gender
gender_abrev gender abrev
hex_color hex color
hex_color_short hex color short
i_pv_4 i pv 4
industry industry
job_title job title
language language
last_name last name
latitude_degrees latitude degrees
latitude_direction latitude direction
latitude_minutes latitude minutes
latitude_seconds latitude seconds
latitude latitude
longitude longitude
longitude_degrees longitude degrees
longitude_direction longitude direction
longitude_minutes longitude minutes
longitude_seconds longitude seconds
male_first_name male first name
male_full_name_with_prefix male full name with prefix
male_full_name_with_suffix male full name with suffix
male_last_name male last name
male_pratronymic male pratronymic
model model
month month
month_num month num
month_short month short
paragraph paragraph
patagraphs patagraphs
patagraphs_n patagraphs of maximum n
password password
patronymic patronymic
phone phone
product product
product_name product name
sentence sentence
sentences sentences
sentences_n sentences of maximum n
simple_pass_word simple pass word
state state
state_abbrev state abbrev
street street
street_address street address
title title
top_level_domain top level domain
user_name user name
week_day week day
week_day_short week day short
week_day_num week day num
word word
words words
words_n words of maximum n
year year
zip zip

Benchmark

This Benchmark uses boom. After registering the sample json above run the following command (Note this is just to check things out, my machine is very slow)

 boom -n 1000 -c 100 http://localhost:3000/api/test

The result

Summary:
  Total:	0.6442 secs.
  Slowest:	0.1451 secs.
  Fastest:	0.0163 secs.
  Average:	0.0586 secs.
  Requests/sec:	1552.3336
  Total Data Received:	39000 bytes.
  Response Size per Request:	39 bytes.

Status code distribution:
  [200]	1000 responses

Response time histogram:
  0.016 [1]	|
  0.029 [121]	|โˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽ
  0.042 [166]	|โˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽ
  0.055 [192]	|โˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽ
  0.068 [192]	|โˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽ
  0.081 [168]	|โˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽ
  0.094 [69]	|โˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽ
  0.106 [41]	|โˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽโˆŽ
  0.119 [22]	|โˆŽโˆŽโˆŽโˆŽ
  0.132 [21]	|โˆŽโˆŽโˆŽโˆŽ
  0.145 [7]	|โˆŽ

Latency distribution:
  10% in 0.0280 secs.
  25% in 0.0364 secs.
  50% in 0.0560 secs.
  75% in 0.0751 secs.
  90% in 0.0922 secs.
  95% in 0.1066 secs.
  99% in 0.1287 secs.

Contributing

Start with clicking the star button to make the author and his neighbors happy. Then fork the repository and submit a pull request for whatever change you want to be added to this project.

If you have any questions, just open an issue.

Author

Geofrey Ernest

Twitter : @gernesti

Licence

This project is released under the MIT licence. See LICENCE for more details.

More Repositories

1

utron

A lightweight MVC framework for Go(Golang)
Go
2,223
star
2

wow

๐Ÿ˜ฎโ—โ— Wowโ— now my Go commandline app is spinning with ๐ŸŒˆ and ๐Ÿด
Go
507
star
3

hero

[NO LONGER MAINTAINED} oauth 2 server implementation in Go
Go
213
star
4

greact

like preact, but for go with wasm
Go
146
star
5

alien

A lightweight and fast http router from outer space
Go
127
star
6

mention

Twitter like mentions and #hashtags parser for Go(Golang)
Go
107
star
7

ita

Go(Golang) library that provides a clean API for dynamically calling structs methods
Go
70
star
8

hot

smart golang templates
Go
61
star
9

front

extracts frontmatter from text files with ease with golang.
Go
55
star
10

qlql

Go
26
star
11

bongo

Elegant static website generation with Go
Go
26
star
12

orange

A lightweight Object Relational Mapper for Go
Go
22
star
13

mailchecker-go

Temporary (disposable/throwaway) email detection library in Go(Golang)
Go
20
star
14

zedlist

DEPRECATED
Go
20
star
15

helen

Handle your static assets with care
Go
18
star
16

zunicode

Zig
17
star
17

hoodie

pure zig language server with swagger and bling bling
Zig
17
star
18

kemi

Win at unpacking archive files with Go (golang)
Go
16
star
19

aurora

minimalistic social network, with bolt database and Go
Go
15
star
20

resingo

[UNMAINTAINED] Unofficial golang sdk for resin.io
Go
14
star
21

time

Zig
14
star
22

talk

What I think
Go
13
star
23

cute

material-ui components for gopherjs and vecty
Go
13
star
24

nutz

A cool way to work with bolt database buckets
Go
12
star
25

vectypresent

Go
12
star
26

base32

base32 encoding/decoding for ziglang
Zig
11
star
27

requiemdb

Pure Go Permanent Storage For Open Telemetry Metrics, Traces and Logs, based on Compressed Roaring B-Tree Bitmaps, using Typescript as Query Language
Go
10
star
28

mpesa-dev-api

Developer guide for M-PESA API
9
star
29

blue

JSON to Influxdb line protocol
Go
9
star
30

awesome-africa

Curated list of awesome projects made in Africa
7
star
31

qlstore

gorilla/sessions storage with embedded sql database( (ql)
Go
6
star
32

url

Zig
6
star
33

semver

4
star
34

frieren

Alternative to prometheus for open telemetry data in (development | testing | staging ) environments built on Compressed Roaring Bitmaps
Go
4
star
35

goo

Win at Go(Golang) with Go
Go
3
star
36

tt

Go
3
star
37

mad

Go
3
star
38

qlfu

Go
3
star
39

lora

A simple website hosting application
CSS
2
star
40

mrs

A user profile manager, with boltdb
Go
2
star
41

wuxia

Go
2
star
42

sequel

Extensible SQL Lexer and Parser for Go
Go
2
star
43

arrow3

Build apache arrow records from protocol buffers
Go
2
star
44

rbf

Go
2
star
45

blogdown

view github based project wiki offline
Ruby
2
star
46

gforms

forms library for golang
Go
2
star
47

gs

Go
1
star
48

adapi

Personal advertisment management API service
Go
1
star
49

classnames

Port of classnames node module to gopherjs/vecty
Go
1
star
50

matrix

Go
1
star
51

glory

millitary grade amunition for your Go web development
Go
1
star
52

trib

Go
1
star
53

sweetjesus

Web backend for M-PESA processing
Go
1
star
54

tafuta

Go
1
star
55

zanzibar

An experimental build tool based oon Go templates.
Go
1
star
56

roaring

Go
1
star
57

rows

Go
1
star
58

jsonrpc2

Zig
1
star
59

image.zig

Image library for zig
Zig
1
star
60

pablo

Build and publish your book
Go
1
star
61

vscode-moon

TypeScript
1
star
62

wudl

Go
1
star
63

bearcub

Go
1
star
64

riddick

Pure Go .DS_Store file reader/writter
Go
1
star
65

legend

Legend of microservices
Go
1
star
66

laisense

Lincense inspector for Go(Golang) project
Go
1
star
67

lily.vim

vim syntax highlighting for lily programming language https://github.com/jesserayadkins/lily
Vim Script
1
star
68

go-wasm-server

JavaScript
1
star