Skip to content

Commit

Permalink
Added Habr demo project (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
luza authored Aug 5, 2019
1 parent 9b9aa59 commit e05dfce
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/traffic-lights-demo/func_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"net/http/httptest"
"testing"

"github.com/lamoda/gonkey/runner"
)

func Test_API(t *testing.T) {
initServer()

srv := httptest.NewServer(nil)

runner.RunWithTesting(t, &runner.RunWithTestingParams{
Server: srv,
TestsDir: "tests/cases",
})
}
75 changes: 75 additions & 0 deletions examples/traffic-lights-demo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)

// возможные состояния светофора
const (
lightRed = "red"
lightYellow = "yellow"
lightGreen = "green"
)

// структура для хранения состояния светофора
type trafficLights struct {
CurrentLight string `json:"currentLight"`
}

// экземпляр светофора
var lights = trafficLights{
CurrentLight: lightRed,
}

func main() {
initServer()

// запуск сервера (блокирующий)
log.Fatal(http.ListenAndServe(":8080", nil))
}

func initServer() {
// метод для получения текущего состояния светофора
http.HandleFunc("/light/get", func(w http.ResponseWriter, r *http.Request) {
resp, err := json.Marshal(lights)
if err != nil {
log.Fatal(err)
}
w.Header().Add("Content-Type", "application/json")
w.Write(resp)
})

// метод для установки нового состояния светофора
http.HandleFunc("/light/set", func(w http.ResponseWriter, r *http.Request) {
request, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}

var newTrafficLights trafficLights
if err := json.Unmarshal(request, &newTrafficLights); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if err := validateRequest(&newTrafficLights); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

lights = newTrafficLights
})
}

func validateRequest(lights *trafficLights) error {
if lights.CurrentLight != lightRed &&
lights.CurrentLight != lightYellow &&
lights.CurrentLight != lightGreen {
return fmt.Errorf("incorrect current light: %s", lights.CurrentLight)
}
return nil
}
18 changes: 18 additions & 0 deletions examples/traffic-lights-demo/tests/cases/light_set_get.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- name: WHEN set is requested MUST return no response
method: POST
path: /light/set
request: >
{
"currentLight": "green"
}
response:
200: ''

- name: WHEN get is requested MUST return green
method: GET
path: /light/get
response:
200: >
{
"currentLight": "green"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
- name: WHEN set is requested MUST return no response
method: POST
path: /light/set
request: >
{
"currentLight": "green"
}
response:
200: ''

- name: WHEN incorrect color is passed MUST return error
method: POST
path: /light/set
request: >
{
"currentLight": "blue"
}
response:
400: >
incorrect current light: blue
- name: WHEN color is missing MUST return error
method: POST
path: /light/set
request: >
{}
response:
400: >
incorrect current light:
- name: WHEN get is requested MUST have color untouched
method: GET
path: /light/get
response:
200: >
{
"currentLight": "green"
}

0 comments on commit e05dfce

Please sign in to comment.