Skip to content

Commit

Permalink
👍 Add test for ValidateResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Omochice committed Sep 30, 2021
1 parent 70c7bd2 commit 8e501c8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
27 changes: 14 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,28 @@ func (c *DeepLClient) Translate(text string, sourceLang string, targetLang strin
return r, nil
}

var KnownErrors = map[int]string{
400: "Bad request. Please check error message and your parameters.",
403: "Authorization failed. Please supply a valid auth_key parameter.",
404: "The requested resource could not be found.",
413: "The request size exceeds the limit.",
414: "The request URL is too long. You can avoid this error by using a POST request instead of a GET request, and sending the parameters in the HTTP body.",
429: "Too many requests. Please wait and resend your request.",
456: "Quota exceeded. The character limit has been reached.",
503: "Resource currently unavailable. Try again later.",
529: "Too many requests. Please wait and resend your request.",
} // this from https://www.deepl.com/docs-api/accessing-the-api/error-handling/

func ValidateResponse(resp *http.Response) error {
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
var data map[string]interface{}
errors := map[int]string{
400: "Bad request. Please check error message and your parameters.",
403: "Authorization failed. Please supply a valid auth_key parameter.",
404: "The requested resource could not be found.",
413: "The request size exceeds the limit.",
414: "The request URL is too long. You can avoid this error by using a POST request instead of a GET request, and sending the parameters in the HTTP body.",
429: "Too many requests. Please wait and resend your request.",
456: "Quota exceeded. The character limit has been reached.",
503: "Resource currently unavailable. Try again later.",
529: "Too many requests. Please wait and resend your request.",
} // this from https://www.deepl.com/docs-api/accessing-the-api/error-handling/
e := json.NewDecoder(resp.Body).Decode(&data)
baseErrorText := fmt.Sprintf("Invalid response [%d %s]",
resp.StatusCode,
http.StatusText(resp.StatusCode))
if t, ok := errors[resp.StatusCode]; ok {
if t, ok := KnownErrors[resp.StatusCode]; ok {
baseErrorText += fmt.Sprintf(" %s", t)
}
e := json.NewDecoder(resp.Body).Decode(&data)
if e != nil {
return fmt.Errorf("%s", baseErrorText)
} else {
Expand Down
57 changes: 56 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
)

Expand Down Expand Up @@ -58,4 +64,53 @@ func TestLoadsettings(t *testing.T) {
}
}

func TestTranslate(t *testing.T) {}
func TestValidateResponse(t *testing.T) {
var errorText string
testErrorMes := map[string]string{
"message": "test message",
}
testBody, err := json.Marshal(testErrorMes)
if err != nil {
t.Fatalf("marshal error")
}
testResponse := http.Response{
Status: "200 test",
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBuffer(testBody)),
}

for c := 100; c < 512; c++ {
if http.StatusText(c) == "" {
// unused stasus code
continue
}
testResponse.Status = fmt.Sprintf("%d test", c)
testResponse.StatusCode = c
err := ValidateResponse(&testResponse)
if c >= 200 && c < 300 {
errorText = "If status code is between 200 and 299, no error should be returned"
if err != nil {
t.Fatalf("%s\nActual: %s", errorText, err.Error())
}
continue
}
if err == nil {
t.Fatalf("If Status code is not 200 <= c < 300, should occur error\nStatus code: %d, Response: %v",
c, testResponse)
} else {
if !strings.Contains(err.Error(), http.StatusText(c)) {
errorText = fmt.Sprintf("Error text should include Status Code(%s)\nActual: %s",
http.StatusText(c), err.Error())
}
if statusText, ok := KnownErrors[c]; ok { // errored
if !strings.Contains(err.Error(), statusText) {
errorText = fmt.Sprintf("If stasus code is knownded, the text should include it's error text\nExpected: %s\nActual: %s",
statusText, err.Error())
t.Fatalf("%s", errorText)
}
}
}
}
// TODO test when body is valid/invalid as json

}

0 comments on commit 8e501c8

Please sign in to comment.