From a4a95e059274153652c11b1a1938bc81a6d53311 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Sat, 11 May 2019 21:22:00 +0430 Subject: [PATCH 1/2] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) 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). From 28a8e1b0de967dfbded5c00d46f0b8d75473ca00 Mon Sep 17 00:00:00 2001 From: ehsan-fmb <43479584+ehsan-fmb@users.noreply.github.com> Date: Fri, 17 May 2019 01:16:48 +0430 Subject: [PATCH 2/2] Create newserver_test.go --- newserver_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 newserver_test.go 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) + } +}