diff --git a/internal/deepl/translate_test.go b/internal/deepl/translate_test.go index d7efe01..b94826b 100644 --- a/internal/deepl/translate_test.go +++ b/internal/deepl/translate_test.go @@ -6,71 +6,79 @@ import ( "net/http" "net/http/httptest" "net/url" - "path/filepath" - "strconv" - "strings" "testing" - - "github.com/candy12t/deepl-cli/test" ) +const defaultBaseURL = "https://api.deepl.com/v2" + func TestTranslate(t *testing.T) { - t.Run("success translate", func(t *testing.T) { - client, server, err := deeplMockServer(t, "test-auth-key", "success-body", "success-header") - if err != nil { - t.Fatal(err) - } - defer server.Close() + client, mux, teardown := setup() + defer teardown() + body := `{"translations":[{"detected_source_language":"EN","text":"こんにちわ"}]}` + want := "こんにちわ" - ctx := context.Background() - got, err := client.Translate(ctx, "hello", "EN", "JA") - if err != nil { - t.Fatal(err) - } - want := "こんにちわ" + mux.HandleFunc(TRANSLATE, func(w http.ResponseWriter, r *http.Request) { + testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded") + testHeader(t, r, "Authorization", "DeepL-Auth-Key test-auth-key") + testMethod(t, r, "POST") + testBody(t, r, "source_lang=EN&target_lang=JA&text=hello") - if got != want { - t.Fatalf("got %v, want %v", got, want) - } + w.WriteHeader(http.StatusOK) + w.Write([]byte(body)) }) + + ctx := context.Background() + got, err := client.Translate(ctx, "hello", "EN", "JA") + assertNoErr(t, err) + assertTranslatedText(t, got, want) } -func deeplMockServer(t *testing.T, mockAuthKey string, mockResponseBody, mockResponseHeader string) (*Client, *httptest.Server, error) { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - header := filepath.Join(test.ProjectDirPath(), "test", "testdata", "deepl", mockResponseHeader) - headerBytes, err := ioutil.ReadFile(header) - h := strings.Split(string(headerBytes), "\n") - for _, line := range h { - if strings.Contains(line, "HTTP") { - statusCode, err := strconv.Atoi(strings.Split(line, " ")[1]) - if err != nil { - t.Fatal(err) - } - w.WriteHeader(statusCode) - } +func setup() (*Client, *http.ServeMux, func()) { + mux := http.NewServeMux() + server := httptest.NewServer(mux) - if strings.Contains(line, "content-type") { - contentType := strings.Split(line, " ")[1] - w.Header().Add("content-type", contentType) - } - } + client, _ := NewClient(defaultBaseURL, "test-auth-key") + url, _ := url.ParseRequestURI(server.URL) + client.BaseURL = url - body := filepath.Join(test.ProjectDirPath(), "test", "testdata", "deepl", mockResponseBody) - bodyBytes, err := ioutil.ReadFile(body) - if err != nil { - t.Fatal(err) - } - w.Write(bodyBytes) - })) + return client, mux, server.Close +} - serverURL, err := url.ParseRequestURI(server.URL) +func testBody(t *testing.T, r *http.Request, want string) { + t.Helper() + b, err := ioutil.ReadAll(r.Body) if err != nil { - return nil, nil, err + t.Errorf("Error reading request body: %v", err) + } + if got := string(b); got != want { + t.Errorf("request Body is %s, want %s", got, want) + } +} + +func testMethod(t *testing.T, r *http.Request, want string) { + t.Helper() + if got := r.Method; got != want { + t.Errorf("Request method: %v, want %v", got, want) } +} + +func testHeader(t *testing.T, r *http.Request, header string, want string) { + t.Helper() + if got := r.Header.Get(header); got != want { + t.Errorf("Header.Get(%q) returned %q, want %q", header, got, want) + } +} - return &Client{ - BaseURL: serverURL, - HTTPClient: server.Client(), - AuthKey: mockAuthKey, - }, server, nil +func assertTranslatedText(t *testing.T, got, want string) { + t.Helper() + if got != want { + t.Errorf("translated text is %s, want %s", got, want) + } +} + +func assertNoErr(t *testing.T, got error) { + t.Helper() + if got != nil { + t.Fatal("got an error but didn't want one") + } }