...

Source file src/google.golang.org/api/internal/examples/fake/fake_test.go

Documentation: google.golang.org/api/internal/examples/fake

     1  // Copyright 2021 Google LLC.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fake
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"net/http"
    11  	"net/http/httptest"
    12  	"testing"
    13  
    14  	"google.golang.org/api/option"
    15  	"google.golang.org/api/translate/v3"
    16  )
    17  
    18  func TestTranslateText(t *testing.T) {
    19  	ctx := context.Background()
    20  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    21  		resp := &translate.TranslateTextResponse{
    22  			Translations: []*translate.Translation{
    23  				{TranslatedText: "Hello World"},
    24  			},
    25  		}
    26  		b, err := json.Marshal(resp)
    27  		if err != nil {
    28  			http.Error(w, "unable to marshal request: "+err.Error(), http.StatusBadRequest)
    29  			return
    30  		}
    31  		w.Write(b)
    32  	}))
    33  	defer ts.Close()
    34  	svc, err := translate.NewService(ctx, option.WithoutAuthentication(), option.WithEndpoint(ts.URL))
    35  	if err != nil {
    36  		t.Fatalf("unable to create client: %v", err)
    37  	}
    38  	text, err := TranslateText(svc, "Hola Mundo", "en-US")
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	if text != "Hello World" {
    43  		t.Fatalf("got %q, want Hello World", text)
    44  	}
    45  }
    46  

View as plain text