-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add test for HandleHTTPError undelete * more verbose http codes * Update Dockerfile Now using go 1.23! Tested this on dev, it works * Update compose_dev.sh Prints initial settings as defined in setup.yaml. Added for usability reasons - users keep getting confused about account balance and maximum allowable debt * add failure condition test * formatting and test standards --------- Co-authored-by: Osnat Katz Moon <137817983+astrosnat@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
94 additions
and
18 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
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,67 @@ | ||
package errors | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestHandleHTTPError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
w *httptest.ResponseRecorder | ||
err error | ||
statusCode int | ||
userMessage string | ||
output string | ||
wrappedMessage string | ||
res bool | ||
}{ | ||
{ | ||
name: "Nil", | ||
w: httptest.NewRecorder(), | ||
err: nil, | ||
statusCode: http.StatusOK, | ||
userMessage: "", | ||
output: "", | ||
wrappedMessage: "", | ||
res: false, | ||
}, | ||
{ | ||
name: "Server Error 500", | ||
w: httptest.NewRecorder(), | ||
err: errors.New("foo"), | ||
statusCode: http.StatusInternalServerError, | ||
userMessage: "bar", | ||
output: "Error: foo", | ||
wrappedMessage: "{\"error\":\"bar\"}", | ||
res: true, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
currentWriter := log.Writer() | ||
var buf bytes.Buffer | ||
log.SetOutput(&buf) | ||
res := HandleHTTPError(test.w, test.err, test.statusCode, test.userMessage) | ||
log.SetOutput(currentWriter) | ||
if test.res != res { | ||
t.Errorf("got %t, want %t", test.res, res) | ||
} | ||
if test.w.Code != test.statusCode { | ||
t.Errorf("got %d, want %d", test.statusCode, test.w.Code) | ||
} | ||
if !strings.Contains(test.w.Body.String(), test.wrappedMessage) { | ||
t.Errorf("got %s, want %s", strings.TrimRight(test.w.Body.String(), "\n"), test.wrappedMessage) | ||
} | ||
if !strings.Contains(buf.String(), test.output) { | ||
t.Errorf("got Error%s, want %s", strings.SplitN(strings.TrimRight(buf.String(), "\n"), "Error", 1)[0], test.output) | ||
} | ||
}) | ||
|
||
} | ||
} |
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
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