github.com/seeker-insurance/kit@v0.0.13/web/testing/integration/testing.go (about) 1 package integration 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/parnurzeal/gorequest" 9 "github.com/seeker-insurance/kit/web/meta" 10 "github.com/seeker-insurance/kit/web/security" 11 ) 12 13 const ( 14 apiURL = "http://localhost:3000" 15 ) 16 17 type ( 18 // JSONAPIRespMeta ... 19 JSONAPIRespMeta struct { 20 Actions []meta.JsonAPIAction 21 } 22 // JSONAPIRespData ... 23 JSONAPIRespData struct { 24 ID string 25 Type string 26 Attributes interface{} `json:"attributes"` 27 Meta JSONAPIRespMeta `json:"meta"` 28 Links interface{} `json:"links"` 29 Relationships interface{} 30 } 31 // JSONAPIOneResp ... 32 JSONAPIOneResp struct { 33 Data JSONAPIRespData `json:"data"` 34 Errors []*interface{} `json:"errors"` 35 Included []JSONAPIRespData 36 } 37 // JSONAPIManyResp ... 38 JSONAPIManyResp struct { 39 Data []JSONAPIRespData `json:"data"` 40 Meta JSONAPIRespMeta `json:"meta"` 41 Errors []*interface{} `json:"errors"` 42 Included []JSONAPIRespData 43 } 44 45 AuthRequester struct { 46 Requester 47 test *testing.T 48 token string 49 } 50 51 Requester struct { 52 } 53 ) 54 55 func NewAuthRequester(t *testing.T, userId int) *AuthRequester { 56 token, err := security.JwtToken(userId) 57 if err != nil { 58 t.Errorf("error getting jwt: %s", err) 59 } 60 return &AuthRequester{token: token, test: t} 61 } 62 63 func (r *AuthRequester) GetList(path string) (gorequest.Response, *JSONAPIManyResp, []error) { 64 resp, body, errs := Request("GET", path, r.token).End() 65 data := unmarshalMany(r.test, body) 66 67 return resp, data, errs 68 } 69 70 // Request generic json api request with optional auth token 71 func Request(method string, path string, token string) (req *gorequest.SuperAgent) { 72 r := gorequest.New(). 73 CustomMethod(method, apiURL+path). 74 Set("Content-Type", "application/vnd.api+json") 75 if token != "" { 76 r = r.Set("Authorization", fmt.Sprintf("Bearer %s", token)) 77 } 78 return r 79 } 80 81 // Get jsonapi get request 82 func Get(t *testing.T, path string, token string) (gorequest.Response, *JSONAPIOneResp, []error) { 83 resp, body, errs := Request("GET", path, token).End() 84 data := unmarshalOne(t, body) 85 86 return resp, data, errs 87 } 88 89 // Post jsonapi post request 90 func Post(t *testing.T, path string, attrs map[string]interface{}, token string) (gorequest.Response, *JSONAPIOneResp, []error) { 91 resp, body, errs := Request("POST", path, token).Send(jsonAPIPayload(t, attrs)).End() 92 data := unmarshalOne(t, body) 93 94 return resp, data, errs 95 } 96 97 // Patch jsonapi patch request 98 func Patch(t *testing.T, path string, attrs map[string]interface{}, token string) (gorequest.Response, *JSONAPIOneResp, []error) { 99 resp, body, errs := Request("PATCH", path, token).Send(jsonAPIPayload(t, attrs)).End() 100 data := unmarshalOne(t, body) 101 102 return resp, data, errs 103 } 104 105 // Delete jsonapi delete request 106 func Delete(t *testing.T, path string, token string) (gorequest.Response, *JSONAPIOneResp, []error) { 107 resp, body, errs := Request("DELETE", path, token).End() 108 109 return resp, unmarshalOne(t, body), errs 110 } 111 112 // GetList jsonapi get list request 113 func GetList(t *testing.T, path string, token string) (gorequest.Response, *JSONAPIManyResp, []error) { 114 resp, body, errs := Request("GET", path, token).End() 115 data := unmarshalMany(t, body) 116 117 return resp, data, errs 118 } 119 120 func jsonAPIPayload(t *testing.T, attrs map[string]interface{}) (payload string) { 121 b, err := json.Marshal(attrs) 122 if err != nil { 123 t.Errorf("Error marshaling payload %v", err) 124 } 125 126 return fmt.Sprintf(` 127 { 128 "data": { 129 "attributes": %s 130 } 131 } 132 `, b) 133 } 134 135 func unmarshalOne(t *testing.T, body string) *JSONAPIOneResp { 136 var data JSONAPIOneResp 137 if body == "" { 138 return &data 139 } 140 err := json.Unmarshal([]byte(body), &data) 141 if err != nil { 142 t.Errorf("Error unmarshaling response %v", err) 143 } 144 145 return &data 146 } 147 148 func unmarshalMany(t *testing.T, body string) *JSONAPIManyResp { 149 var data JSONAPIManyResp 150 err := json.Unmarshal([]byte(body), &data) 151 if err != nil { 152 t.Errorf("Error unmarshaling response %v", err) 153 } 154 155 return &data 156 }