/* * Hexacode Request * This program is Http Request Client * Package: gitlab.hexacode.org/go-libs/requests * Maintainer: Azzis Arswendo * * Copyright (C) 2023 Hexacode Teknologi Indonesia * All Rights Reserved */ package requests import ( "net/http" "net/http/httptest" "reflect" "testing" "gitlab.hexacode.org/go-libs/hctypes" ) func TestRequest(t *testing.T) { // Create a mock server for testing server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Write a mock response w.WriteHeader(StatusOK) w.Write([]byte("Mock Response")) })) defer server.Close() // Create an Input for the request in := &Input{ Method: "GET", URL: server.URL, URLParams: nil, Headers: nil, Body: hctypes.Buffer{}, } // Call the function being tested output, err := Request(in) // Assert that the response status, status text, and body match the expected values expectedOutput := &Output{ Status: StatusOK, StatusText: StatusText(StatusOK), Headers: nil, Body: hctypes.Buffer("Mock Response"), } if !reflect.DeepEqual(output.Status, expectedOutput.Status) { t.Errorf("Unexpected `output.Status`. Expected \"%v\", got \"%v\"", expectedOutput.Status, output.Status) } if !reflect.DeepEqual(output.StatusText, expectedOutput.StatusText) { t.Errorf("Unexpected `output.StatusText`. Expected \"%v\", got \"%v\"", expectedOutput.StatusText, output.StatusText) } if !output.Body.Equal(expectedOutput.Body) { t.Errorf("Unexpected `output.Body`. Expected \"%v\", got \"%v\"", expectedOutput.Body.ToString(), output.Body.ToString()) } // Assert that the error is nil if err != nil { t.Errorf("Unexpected error: %v", err) } }