github.heygears.com/openimsdk/tools@v0.0.49/utils/httputil/http_client_test.go (about) 1 // Copyright © 2024 OpenIM open source community. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package httputil 16 17 import ( 18 "context" 19 "encoding/json" 20 "io" 21 "net/http" 22 "net/http/httptest" 23 "reflect" 24 "testing" 25 ) 26 27 func TestHTTPClient_Get(t *testing.T) { 28 // Setup a mock server 29 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 30 w.WriteHeader(http.StatusOK) 31 w.Write([]byte("mock response")) 32 })) 33 defer server.Close() 34 35 client := NewHTTPClient(NewClientConfig()) 36 body, err := client.Get(server.URL) 37 if err != nil { 38 t.Fatalf("Expected no error, got %v", err) 39 } 40 if string(body) != "mock response" { 41 t.Fatalf("Expected 'mock response', got %s", body) 42 } 43 } 44 45 func TestHTTPClient_Post(t *testing.T) { 46 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 47 if r.Method != http.MethodPost { 48 t.Fatalf("Expected POST method, got %s", r.Method) 49 } 50 body, err := io.ReadAll(r.Body) 51 if err != nil { 52 t.Fatalf("Failed to read request body: %v", err) 53 } 54 w.Write(body) 55 })) 56 defer server.Close() 57 58 client := NewHTTPClient(NewClientConfig()) 59 headers := map[string]string{"Custom-Header": "value"} 60 61 expectedData := map[string]string{"key": "value"} 62 respBody, err := client.Post(context.Background(), server.URL, headers, expectedData, 10) 63 if err != nil { 64 t.Fatalf("Expected no error, got %v", err) 65 } 66 67 var expected, actual map[string]any 68 if err := json.Unmarshal([]byte(`{"key":"value"}`), &expected); err != nil { 69 t.Fatalf("Error unmarshaling expected JSON: %v", err) 70 } 71 if err := json.Unmarshal(respBody, &actual); err != nil { 72 t.Fatalf("Error unmarshaling actual response body: %v", err) 73 } 74 75 if !reflect.DeepEqual(expected, actual) { 76 t.Fatalf("Expected %v, got %v", expected, actual) 77 } 78 } 79 80 func TestHTTPClient_PostReturn(t *testing.T) { 81 expectedOutput := struct{ Key string }{"value"} 82 83 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 84 json.NewEncoder(w).Encode(expectedOutput) 85 })) 86 defer server.Close() 87 88 client := NewHTTPClient(NewClientConfig()) 89 var actualOutput struct{ Key string } 90 err := client.PostReturn(context.Background(), server.URL, nil, map[string]string{"key": "value"}, &actualOutput, 10) 91 if err != nil { 92 t.Fatalf("Expected no error, got %v", err) 93 } 94 if actualOutput.Key != expectedOutput.Key { 95 t.Fatalf("Expected %s, got %s", expectedOutput.Key, actualOutput.Key) 96 } 97 }