Skip to content

Commit

Permalink
feat: update test file
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj319 committed Dec 30, 2023
1 parent 123d0ae commit bed28f8
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions nirajan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package nirajan

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

func TestSimpleRouter(t *testing.T) {
router := CreateRouter()

router.AddRoute("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}, GET)

router.AddRoute("/users/:Id", func(w http.ResponseWriter, r *http.Request, params struct{ Id string }) {
if params.Id != "1" {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}, GET)
for routeObj := range router.routeMapping {
fmt.Println(routeObj.route, routeObj.pathParams)
}

req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

req, err = http.NewRequest("GET", "/users/1", nil)
if err != nil {
t.Fatal(err)
}

rr = httptest.NewRecorder()
router.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

req, err = http.NewRequest("GET", "/users/2", nil)
if err != nil {
t.Fatal(err)
}

rr = httptest.NewRecorder()
router.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}
}

0 comments on commit bed28f8

Please sign in to comment.