diff --git a/examples/traffic-lights-demo/func_test.go b/examples/traffic-lights-demo/func_test.go new file mode 100644 index 0000000..75dcb87 --- /dev/null +++ b/examples/traffic-lights-demo/func_test.go @@ -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", + }) +} diff --git a/examples/traffic-lights-demo/main.go b/examples/traffic-lights-demo/main.go new file mode 100644 index 0000000..3fccb14 --- /dev/null +++ b/examples/traffic-lights-demo/main.go @@ -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 +} diff --git a/examples/traffic-lights-demo/tests/cases/light_set_get.yaml b/examples/traffic-lights-demo/tests/cases/light_set_get.yaml new file mode 100644 index 0000000..053c734 --- /dev/null +++ b/examples/traffic-lights-demo/tests/cases/light_set_get.yaml @@ -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" + } diff --git a/examples/traffic-lights-demo/tests/cases/light_set_get_negative.yaml b/examples/traffic-lights-demo/tests/cases/light_set_get_negative.yaml new file mode 100644 index 0000000..dcf827f --- /dev/null +++ b/examples/traffic-lights-demo/tests/cases/light_set_get_negative.yaml @@ -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" + } +