-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Fix error response with no body (#365)
Unauthenticated client handles error response without body
- Loading branch information
Showing
4 changed files
with
127 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package login | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/launchdarkly/ldcli/internal/errors" | ||
) | ||
|
||
type UnauthenticatedClient interface { | ||
MakeRequest( | ||
method string, | ||
path string, | ||
data []byte, | ||
) ([]byte, error) | ||
} | ||
|
||
type Client struct { | ||
cliVersion string | ||
} | ||
|
||
func NewClient(cliVersion string) Client { | ||
return Client{ | ||
cliVersion: cliVersion, | ||
} | ||
} | ||
|
||
func (c Client) MakeRequest( | ||
method string, | ||
path string, | ||
data []byte, | ||
) ([]byte, error) { | ||
client := http.Client{} | ||
req, _ := http.NewRequest(method, path, bytes.NewReader(data)) | ||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Set("User-Agent", fmt.Sprintf("launchdarkly-cli/v%s", c.cliVersion)) | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode < http.StatusBadRequest { | ||
return body, nil | ||
} | ||
|
||
if len(body) > 0 { | ||
return body, errors.NewError(string(body)) | ||
} | ||
|
||
switch res.StatusCode { | ||
case http.StatusMethodNotAllowed: | ||
resp, _ := json.Marshal(map[string]string{ | ||
"code": "method_not_allowed", | ||
"message": "method not allowed", | ||
}) | ||
return body, errors.NewError(string(resp)) | ||
default: | ||
return body, errors.NewError(fmt.Sprintf("could not complete the request: %d", res.StatusCode)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package login_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/launchdarkly/ldcli/internal/login" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMakeRequest(t *testing.T) { | ||
t.Run("with a successful response", func(t *testing.T) { | ||
server := makeServer(t, http.StatusOK, `{"message": "success"}`) | ||
defer server.Close() | ||
c := login.NewClient("test-version") | ||
|
||
response, err := c.MakeRequest("POST", server.URL, []byte(`{}`)) | ||
|
||
require.NoError(t, err) | ||
assert.JSONEq(t, `{"message": "success"}`, string(response)) | ||
}) | ||
|
||
t.Run("with an error with a response body", func(t *testing.T) { | ||
server := makeServer(t, http.StatusBadRequest, `{"message": "an error"}`) | ||
defer server.Close() | ||
c := login.NewClient("test-version") | ||
|
||
_, err := c.MakeRequest("POST", server.URL, []byte(`{}`)) | ||
|
||
assert.EqualError(t, err, `{"message": "an error"}`) | ||
}) | ||
|
||
t.Run("with a method not allowed status code and no body", func(t *testing.T) { | ||
server := makeServer(t, http.StatusMethodNotAllowed, "") | ||
defer server.Close() | ||
c := login.NewClient("test-version") | ||
expectedResponse := `{ | ||
"code": "method_not_allowed", | ||
"message": "method not allowed" | ||
}` | ||
|
||
_, err := c.MakeRequest("POST", server.URL, []byte(`{}`)) | ||
|
||
assert.JSONEq(t, expectedResponse, err.Error()) | ||
}) | ||
} | ||
|
||
func makeServer(t *testing.T, statusResponse int, responseBody string) *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "application/json", r.Header.Get("Content-Type")) | ||
assert.Equal(t, "launchdarkly-cli/vtest-version", r.Header.Get("User-Agent")) | ||
w.WriteHeader(statusResponse) | ||
_, _ = w.Write([]byte(responseBody)) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters