1+ package main
2+
3+ import (
4+ "bytes"
5+ "github.com/stretchr/testify/assert"
6+ "io"
7+ "net/http"
8+ "testing"
9+ "time"
10+ )
11+
12+ func TestRest (t * testing.T ) {
13+ testData := []struct {
14+ path string
15+ method string
16+ body string
17+ status int
18+ expected string
19+ }{
20+ {"/bookshelf" , http .MethodGet , "" , 200 , `[{"id":1,"name":"Blue Bird","pages":500}]` },
21+ {"/bookshelf/1" , http .MethodGet , "" , 200 , `{"id":1,"name":"Blue Bird","pages":500}` },
22+ {"/bookshelf/2" , http .MethodGet , "" , 404 , `{"message":"book not found"}` },
23+ {"/bookshelf" , http .MethodPost , `{"name":"Blue Bird","pages":500}` , 409 , `{"message":"duplicate book name"}` },
24+ {"/bookshelf" , http .MethodPost , `{"name":"Red Bird","pages":500}` , 201 , `{"id":2,"name":"Red Bird","pages":500}` },
25+ {"/bookshelf" , http .MethodGet , "" , 200 , `[{"id":1,"name":"Blue Bird","pages":500},{"id":2,"name":"Red Bird","pages":500}]` },
26+ {"/bookshelf/2" , http .MethodGet , "" , 200 , `{"id":2,"name":"Red Bird","pages":500}` },
27+ {"/bookshelf/2" , http .MethodPut , `{"name":"Yellow Bird","pages":500}` , 200 , `{"id":2,"name":"Yellow Bird","pages":500}` },
28+ {"/bookshelf/2" , http .MethodGet , "" , 200 , `{"id":2,"name":"Yellow Bird","pages":500}` },
29+ {"/bookshelf/2" , http .MethodPut , `{"name":"Blue Bird","pages":500}` , 409 , `{"message":"duplicate book name"}` },
30+ {"/bookshelf/300" , http .MethodPut , `{"name":"Real life","pages":500}` , 404 , `{"message":"book not found"}` },
31+ {"/bookshelf/2" , http .MethodDelete , "" , 204 , `` },
32+ {"/bookshelf/800" , http .MethodDelete , "" , 204 , `` },
33+ {"/bookshelf/2" , http .MethodGet , "" , 404 , `{"message":"book not found"}` },
34+ {"/bookshelf" , http .MethodGet , "" , 200 , `[{"id":1,"name":"Blue Bird","pages":500}]` },
35+ {"/bookshelf" , http .MethodPost , `{"name":"Les Misérables","pages":500}` , 201 , `{"id":3,"name":"Les Misérables","pages":500}` },
36+ {"/bookshelf" , http .MethodGet , "" , 200 , `[{"id":1,"name":"Blue Bird","pages":500},{"id":3,"name":"Les Misérables","pages":500}]` },
37+ {"/bookshelf/3" , http .MethodGet , "" , 200 , `{"id":3,"name":"Les Misérables","pages":500}` },
38+ {"/bookshelf/3" , http .MethodPut , `{"name":"Red Bird","pages":1000}` , 200 , `{"id":3,"name":"Red Bird","pages":1000}` },
39+ {"/bookshelf/3" , http .MethodGet , "" , 200 , `{"id":3,"name":"Red Bird","pages":1000}` },
40+ {"/bookshelf/3" , http .MethodDelete , "" , 204 , `` },
41+ {"/bookshelf/2" , http .MethodDelete , "" , 204 , `` },
42+ {"/bookshelf/1" , http .MethodDelete , "" , 204 , `` },
43+ {"/bookshelf" , http .MethodGet , "" , 200 , `[]` },
44+ }
45+
46+ go func () {
47+ main ()
48+ }()
49+
50+ isUp := false
51+ for i := 0 ; i < 30 ; i ++ {
52+ _ , err := http .Get ("http://localhost:8087" )
53+ if err == nil {
54+ isUp = true
55+ break
56+ }
57+ t .Logf ("Server not up yet... try again" )
58+ time .Sleep (2 * time .Second )
59+ }
60+ if ! isUp {
61+ t .Fatal ("Unable to connect to server" )
62+ return
63+ }
64+
65+ for _ , tc := range testData {
66+ bodyVal := tc .body
67+ if bodyVal == "" {
68+ bodyVal = "nil"
69+ }
70+ t .Logf ("Sending request... Path: \" %s\" \t Method: %s\t Body: %s" , tc .path , tc .method , bodyVal )
71+
72+ req , err := http .NewRequest (tc .method , "http://localhost:8087" + tc .path , bytes .NewReader ([]byte (tc .body )))
73+ if err != nil {
74+ t .Fatal (err )
75+ return
76+ }
77+ resp , err := http .DefaultClient .Do (req )
78+ if err != nil {
79+ t .Fatal (err )
80+ return
81+ }
82+
83+ if ! assert .Equal (t , tc .status , resp .StatusCode ) {
84+ return
85+ }
86+
87+ body , err := io .ReadAll (resp .Body )
88+ if err != nil {
89+ t .Fatal (err )
90+ return
91+ }
92+ if ! assert .Equal (t , tc .expected , string (body )) {
93+ return
94+ }
95+
96+ err = resp .Body .Close ()
97+ if err != nil {
98+ t .Fatal (err )
99+ return
100+ }
101+ }
102+ }
0 commit comments