Skip to content

Commit

Permalink
pass headers, fix get bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsakos committed Dec 28, 2023
1 parent 057d4a0 commit de466c4
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions test_rest_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@ import (

var Handler http.Handler

type Client struct{
Headers map[string]string
}


func init(){
if Handler == nil{
Handler = http.DefaultServeMux
}
}

func (m Client) GetEndpoint(path string, responseStruct interface{}, idToken string) error {
func GetEndpoint(path string, headers map[string]string, responseStruct interface{}) error {

req := httptest.NewRequest(http.MethodGet, path, nil)

if idToken != "" {
req.Header.Set("idToken", idToken)
for key, val := range headers {
req.Header.Set(key, val)
}

w := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(w, req)
Handler.ServeHTTP(w, req)
res := w.Result()
defer res.Body.Close()

Expand All @@ -46,7 +44,7 @@ func (m Client) GetEndpoint(path string, responseStruct interface{}, idToken str
fmt.Print(string(data))

if res.StatusCode != 200 {
return fmt.Errorf("%s %s", res.Status, string(data))
return fmt.Errorf("%s %s %s", path, res.Status, string(data))
}
if responseStruct != nil {
err = json.Unmarshal(data, responseStruct)
Expand All @@ -55,16 +53,16 @@ func (m Client) GetEndpoint(path string, responseStruct interface{}, idToken str
return err
}

func (m Client) PostEndpoint(path string, body interface{}, responseStruct interface{}, idToken string) error {
return postOrPutEndpoint(http.MethodPost, m.Headers, path, body, responseStruct, idToken)
func PostEndpoint(path string, headers map[string]string, body interface{}, responseStruct interface{}) error {
return postOrPutEndpoint(http.MethodPost, headers, path, body, responseStruct)
}
func (m Client) PutEndpoint(path string, body interface{}, responseStruct interface{}, idToken string) error {
return postOrPutEndpoint(http.MethodPut, m.Headers, path, body, responseStruct, idToken)
func PutEndpoint(path string, headers map[string]string, body interface{}, responseStruct interface{}) error {
return postOrPutEndpoint(http.MethodPut, headers, path, body, responseStruct)
}
func (m Client) DeleteEndpoint(path string, body interface{}, responseStruct interface{}, idToken string) error {
return postOrPutEndpoint(http.MethodDelete, m.Headers, path, body, responseStruct, idToken)
func DeleteEndpoint(path string, headers map[string]string, body interface{}, responseStruct interface{}) error {
return postOrPutEndpoint(http.MethodDelete, headers, path, body, responseStruct)
}
func postOrPutEndpoint(method string, headers map[string]string, path string, body interface{}, responseStruct interface{}, idToken string) error {
func postOrPutEndpoint(method string, headers map[string]string, path string, body interface{}, responseStruct interface{}) error {

var req *http.Request

Expand Down Expand Up @@ -93,9 +91,6 @@ func postOrPutEndpoint(method string, headers map[string]string, path string, bo
for key, val := range headers {
req.Header.Set(key, val)
}
if idToken != "" {
req.Header.Set("idToken", idToken)
}

w := httptest.NewRecorder()
Handler.ServeHTTP(w, req)
Expand Down

0 comments on commit de466c4

Please sign in to comment.