diff --git a/README.md b/README.md index f8b912b..87fba72 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,7 @@ that is named `ping` and returns no content with 200 status. } ``` +4. Use [golangci-lint](https://github.com/golangci/golangci-lint) to lint your code :rocket: +5. Write tests for your endpoints. In the tests you must use MongoDB and check your recently created record. + For conntecting to Mongodb use the offical driver that can be found [here](https://github.com/mongodb/mongo-go-driver). diff --git a/newserver_test.go b/newserver_test.go new file mode 100644 index 0000000..86c90c3 --- /dev/null +++ b/newserver_test.go @@ -0,0 +1,46 @@ +package testing101 +import( + "bytes" + "testing" + "net/http" + "github.com/labstack/echo" + "net/http/httptest" +) +func TestPinging(t *testing.T) { + e := echo.New() + e.GET("/ping",Pinging) + req, err := http.NewRequest("GET", "/ping", nil) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + e.ServeHTTP(rr, req) + status := rr.Code + if status != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", + status, http.StatusOK) + } + if status == http.StatusOK { + t.Errorf("handler returned expected body: got %v with status %v",rr.Body.String(),status) + } +} +func TestPosting(t *testing.T){ + e := echo.New() + e.POST("/posting",JsonHandler) + var jsonStr = []byte(`{"lat":"20ms","lng":"english"}`) + req, err := http.NewRequest("POST", "/posting", bytes.NewBuffer(jsonStr)) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Content-Type", "application/json") + rr := httptest.NewRecorder() + e.ServeHTTP(rr, req) + status := rr.Code + if status != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", + status,http.StatusOK) + } + if status == http.StatusOK { + t.Errorf("handler returned expected body: got %v with status %v",rr.Body.String(),status) + } +}