| 100 | } |
| 101 | |
| 102 | func (s *TestingServer) request(method, uri string, body io.Reader, params, header map[string]string) (io.ReadCloser, error) { |
| 103 | fullURL := fmt.Sprintf("http://localhost:%d%s", s.profile.Port, uri) |
| 104 | req, err := http.NewRequest(method, fullURL, body) |
| 105 | if err != nil { |
| 106 | return nil, errors.Wrapf(err, "fail to create a new %s request(%q)", method, fullURL) |
| 107 | } |
| 108 | |
| 109 | for k, v := range header { |
| 110 | req.Header.Set(k, v) |
| 111 | } |
| 112 | |
| 113 | q := url.Values{} |
| 114 | for k, v := range params { |
| 115 | q.Add(k, v) |
| 116 | } |
| 117 | if len(q) > 0 { |
| 118 | req.URL.RawQuery = q.Encode() |
| 119 | } |
| 120 | |
| 121 | resp, err := s.client.Do(req) |
| 122 | if err != nil { |
| 123 | return nil, errors.Wrapf(err, "fail to send a %s request(%q)", method, fullURL) |
| 124 | } |
| 125 | if resp.StatusCode != http.StatusOK { |
| 126 | body, err := io.ReadAll(resp.Body) |
| 127 | if err != nil { |
| 128 | return nil, errors.Wrap(err, "failed to read http response body") |
| 129 | } |
| 130 | return nil, errors.Errorf("http response error code %v body %q", resp.StatusCode, string(body)) |
| 131 | } |
| 132 | |
| 133 | if method == "POST" { |
| 134 | if strings.Contains(uri, "/api/v1/auth/login") || strings.Contains(uri, "/api/v1/auth/signup") { |
| 135 | cookie := "" |
| 136 | h := resp.Header.Get("Set-Cookie") |
| 137 | parts := strings.Split(h, "; ") |
| 138 | for _, p := range parts { |
| 139 | if strings.HasPrefix(p, fmt.Sprintf("%s=", auth.AccessTokenCookieName)) { |
| 140 | cookie = p |
| 141 | break |
| 142 | } |
| 143 | } |
| 144 | if cookie == "" { |
| 145 | return nil, errors.New("unable to find access token in the login response headers") |
| 146 | } |
| 147 | s.cookie = cookie |
| 148 | } else if strings.Contains(uri, "/api/v1/auth/signout") { |
| 149 | s.cookie = "" |
| 150 | } |
| 151 | } |
| 152 | return resp.Body, nil |
| 153 | } |
| 154 | |
| 155 | // get sends a GET client request. |
| 156 | func (s *TestingServer) get(url string, params map[string]string) (io.ReadCloser, error) { |