Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Nov 24, 2015
0 parents commit 46d825d
Show file tree
Hide file tree
Showing 13 changed files with 889 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/
19 changes: 19 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015 Geofrey Ernest <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test :
@go test -cover

deps:
@go get github.com/mitchellh/gox

dist:
@gox -output="bin/{{.Dir}}v$(VERSION)_{{.OS}}_{{.Arch}}" ./cmd/apidemic
114 changes: 114 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# ApiDemic

Apidemic is a service for generating fake JSON API 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 nardcoding the sample json api response in tests. I 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._

# Instalation

You can downloaad the binaries for your respective operating system [Download apidemic]()

Then put the downloaaded binary somewhere in your system path.

Alternatively, if you have golang installed

go get fithub.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

```json
{
"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 alreasy started apidemic server you can register that response by making a POST request to the `/register` path. Passing the json body of the form.

```json
{
"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:ountry": "Sweden"
}
}

}
```

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

I f your post request is submitted you are good to ask for the response with fake values. Just do a get request for the endppint you registered

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

# Author
Geofrey Ernest

Twitter : [@gernesti](https://twitter.com/gernesti)


# Licence

This project is released under the MIT licence. See [LICENCE](LICENCE) for more details.
91 changes: 91 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package apidemic

import (
"encoding/json"
"log"
"net/http"
"time"

"github.com/gorilla/mux"
"github.com/pmylund/go-cache"
)

const Version = "0.1"

var maxItemTime = cache.DefaultExpiration

var store = func() *cache.Cache {
c := cache.New(5*time.Minute, 30*time.Second)
return c
}()

type API struct {
Endpoint string `json:"endpoint"`
Payload map[string]interface{} `json:"payload"`
}

func Home(w http.ResponseWriter, r *http.Request) {
details := make(map[string]interface{})
details["app_name"] = "ApiDemic"
details["version"] = Version
details["details"] = "Fake JSON API response"
RenderJSON(w, http.StatusOK, details)
return
}

func RenderJSON(w http.ResponseWriter, code int, value interface{}) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(value)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func RegisterEndpoint(w http.ResponseWriter, r *http.Request) {
a := API{}
err := json.NewDecoder(r.Body).Decode(&a)
if err != nil {
RenderJSON(w, http.StatusInternalServerError, NewResponse(err.Error()))
return
}
if _, ok := store.Get(a.Endpoint); ok {
RenderJSON(w, http.StatusOK, NewResponse("endpoint already taken"))
return
}
obj := NewObject()
err = obj.Load(a.Payload)
if err != nil {
RenderJSON(w, http.StatusInternalServerError, NewResponse(err.Error()))
return
}
log.Println(a.Endpoint)
store.Set(a.Endpoint, obj, maxItemTime)
RenderJSON(w, http.StatusOK, NewResponse("cool"))
}

func GetEndpoint(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
endpoint := vars["endpoint"]
if eVal, ok := store.Get(endpoint); ok {
RenderJSON(w, http.StatusOK, eVal)
return
}
RenderJSON(w, http.StatusNotFound, NewResponse("apidemic: "+endpoint+" is not found"))
}

func NewResponse(message string) interface{} {
return struct {
Text string `json:"text"`
}{
message,
}
}

func NewServer() *mux.Router {
m := mux.NewRouter()
m.HandleFunc("/", Home)
m.HandleFunc("/register", RegisterEndpoint).Methods("POST")
m.HandleFunc("/api/{endpoint}", GetEndpoint).Methods("GET")
return m
}
51 changes: 51 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package apidemic

import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)

func TestAPI(t *testing.T) {
s := NewServer()
sample, err := ioutil.ReadFile("fixtures/sample_request.json")
if err != nil {
t.Fatal(err)
}
var out map[string]interface{}
err = json.NewDecoder(bytes.NewReader(sample)).Decode(&out)
if err != nil {
t.Fatal(err)
}

w := httptest.NewRecorder()
req := jsonRequest("POST", "/register", out)

s.ServeHTTP(w, req)

w = httptest.NewRecorder()
req = jsonRequest("GET", "/api/test", nil)

s.ServeHTTP(w, req)
}

func jsonRequest(method string, path string, body interface{}) *http.Request {
var bEnd io.Reader
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil
}
bEnd = bytes.NewReader(b)
}
req, err := http.NewRequest(method, path, bEnd)
if err != nil {
panic(err)
}
req.Header.Set("Contet-Type", "application/json")
return req
}
45 changes: 45 additions & 0 deletions cmd/apidemic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"log"
"net/http"

"github.com/codegangsta/cli"
"github.com/gernest/apidemic"
)

func server(ctx *cli.Context) {
port := ctx.Int("port")
s := apidemic.NewServer()

log.Println("starting server on port :", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), s))
}

func main() {
app := cli.NewApp()
app.Name = "apidemic"
app.Usage = "Fake JSON API Responses"
app.Authors = []cli.Author{
{"Geofrey Ernest", "[email protected]"},
}
app.Version = apidemic.Version
app.Commands = []cli.Command{
cli.Command{
Name: "start",
ShortName: "s",
Usage: "starts apidemic server",
Action: server,
Flags: []cli.Flag{
cli.IntFlag{
Name: "port",
Usage: "http port to run",
Value: 3000,
EnvVar: "POSRT",
},
},
},
}
app.RunAndExitOnError()
}
28 changes: 28 additions & 0 deletions fixtures/response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"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"
}
}
28 changes: 28 additions & 0 deletions fixtures/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"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:ountry": "Sweden"
}
}
Loading

0 comments on commit 46d825d

Please sign in to comment.