-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
examples/traffic-lights-demo/tests/cases/light_set_get.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
39 changes: 39 additions & 0 deletions
39
examples/traffic-lights-demo/tests/cases/light_set_get_negative.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||