...

Source file src/gitlab.hexacode.org/go-libs/requests/requests_test.go

Documentation: gitlab.hexacode.org/go-libs/requests

     1  /*
     2   * Hexacode Request
     3   * This program is Http Request Client
     4   * Package: gitlab.hexacode.org/go-libs/requests
     5   * Maintainer: Azzis Arswendo <azzis@hexacode.org>
     6   *
     7   * Copyright (C) 2023 Hexacode Teknologi Indonesia
     8   * All Rights Reserved
     9   */
    10  
    11  package requests
    12  
    13  import (
    14  	"net/http"
    15  	"net/http/httptest"
    16  	"reflect"
    17  	"testing"
    18  
    19  	"gitlab.hexacode.org/go-libs/hctypes"
    20  )
    21  
    22  func TestRequest(t *testing.T) {
    23  	// Create a mock server for testing
    24  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    25  		// Write a mock response
    26  		w.WriteHeader(StatusOK)
    27  		w.Write([]byte("Mock Response"))
    28  	}))
    29  	defer server.Close()
    30  
    31  	// Create an Input for the request
    32  	in := &Input{
    33  		Method:    "GET",
    34  		URL:       server.URL,
    35  		URLParams: nil,
    36  		Headers:   nil,
    37  		Body:      hctypes.Buffer{},
    38  	}
    39  
    40  	// Call the function being tested
    41  	output, err := Request(in)
    42  
    43  	// Assert that the response status, status text, and body match the expected values
    44  	expectedOutput := &Output{
    45  		Status:     StatusOK,
    46  		StatusText: StatusText(StatusOK),
    47  		Headers:    nil,
    48  		Body:       hctypes.Buffer("Mock Response"),
    49  	}
    50  
    51  	if !reflect.DeepEqual(output.Status, expectedOutput.Status) {
    52  		t.Errorf("Unexpected `output.Status`. Expected \"%v\", got \"%v\"", expectedOutput.Status, output.Status)
    53  	}
    54  	if !reflect.DeepEqual(output.StatusText, expectedOutput.StatusText) {
    55  		t.Errorf("Unexpected `output.StatusText`. Expected \"%v\", got \"%v\"", expectedOutput.StatusText, output.StatusText)
    56  	}
    57  	if !output.Body.Equal(expectedOutput.Body) {
    58  		t.Errorf("Unexpected `output.Body`. Expected \"%v\", got \"%v\"", expectedOutput.Body.ToString(), output.Body.ToString())
    59  	}
    60  
    61  	// Assert that the error is nil
    62  	if err != nil {
    63  		t.Errorf("Unexpected error: %v", err)
    64  	}
    65  }
    66  

View as plain text