-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
54 lines (46 loc) · 1.21 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestTranslateHandler(t *testing.T) {
// Create a test request payload
requestPayload := TranslateRequest{
Text: "Hello",
To: "es",
}
payloadBytes, _ := json.Marshal(requestPayload)
// Create a test request
req, err := http.NewRequest("POST", "/translate", bytes.NewBuffer(payloadBytes))
if err != nil {
t.Fatal(err)
}
// Create a response recorder to capture the response
rr := httptest.NewRecorder()
// Call the TranslateHandler function directly
handler := http.HandlerFunc(TranslateHandler)
handler.ServeHTTP(rr, req)
// Check the response status code
if rr.Code != http.StatusOK {
t.Errorf("Expected status code %v, but got %v", http.StatusOK, rr.Code)
}
// Parse the response body
var response TranslateResponse
err = json.Unmarshal(rr.Body.Bytes(), &response)
if err != nil {
t.Errorf("Failed to unmarshal response body: %v", err)
}
// Check the response fields
if response.TranslatedText == "" {
t.Error("TranslatedText field is empty")
}
if response.Status != true {
t.Error("Status field is not true")
}
if response.Message != "" {
t.Error("Message field is not empty")
}
}