github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/test/mock/client/http.go (about)

     1  package mock
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  type MockedHttpClient struct {
    10  	BaseURL string
    11  
    12  	RequestMethodResponse           []byte
    13  	RequestMethodResponseStatusCode int
    14  	RequestMethodError              error
    15  }
    16  
    17  // SetOauthToken method sets the oauth token and retrieves its self
    18  func (client *MockedHttpClient) SetOauthToken(token string) {
    19  
    20  }
    21  
    22  // GetClientID method retrieves the clientID
    23  func (client MockedHttpClient) GetClientID() string {
    24  	return "client.ClientID"
    25  }
    26  
    27  // GetClientSecret method retrieves the clientSecret
    28  func (client MockedHttpClient) GetClientSecret() string {
    29  	return "client.ClientSecret"
    30  }
    31  
    32  // GetOAuthToken method retrieves the oauth token
    33  func (client MockedHttpClient) GetOAuthToken() string {
    34  	return "client.OAuthToken"
    35  }
    36  
    37  // SetBaseURL method sets the base url and retrieves its self
    38  func (client *MockedHttpClient) SetBaseURL(baseUrl string) {
    39  
    40  }
    41  
    42  func (client MockedHttpClient) BasicAuth(username string, password string) string {
    43  	return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
    44  }
    45  
    46  // Request method for API requests
    47  //
    48  // This method accepts parameters:
    49  // method - the method of request. Ex: POST, GET, PUT, DELETE and etc
    50  // endpoint - endpoint to which we should do a request
    51  // body - it's a request body. Accepted types of body: string, url.Values(for form_data requests), byte
    52  // headers - request headers
    53  func (client MockedHttpClient) Request(method string, endpoint string, body interface{}, headers map[string]string) ([]byte, int, error) {
    54  	return client.RequestMethodResponse, client.RequestMethodResponseStatusCode, client.RequestMethodError
    55  }
    56  
    57  // Post method for POST http requests
    58  func (client MockedHttpClient) Post(endpoint string, body interface{}, headers map[string]string) ([]byte, int, error) {
    59  	return client.Request(http.MethodPost, client.generateAPIUrl(endpoint), body, headers)
    60  }
    61  
    62  // Put method for PUT http requests
    63  func (client MockedHttpClient) Put(endpoint string, body interface{}, headers map[string]string) ([]byte, int, error) {
    64  	return client.Request(http.MethodPut, client.generateAPIUrl(endpoint), body, headers)
    65  }
    66  
    67  // Get method for GET http requests
    68  func (client *MockedHttpClient) Get(endpoint string, query map[string]string) ([]byte, int, error) {
    69  	var queryString = ""
    70  	for fieldName, value := range query {
    71  		if queryString == "" {
    72  			queryString += "?"
    73  		} else {
    74  			queryString += "&"
    75  		}
    76  
    77  		queryString += fmt.Sprintf("%s=%s", fieldName, value)
    78  	}
    79  
    80  	return client.Request(http.MethodGet, client.generateAPIUrl(endpoint)+fmt.Sprintf("%s", queryString), []byte(``), map[string]string{})
    81  }
    82  
    83  func (client MockedHttpClient) generateAPIUrl(endpoint string) string {
    84  	return client.BaseURL + endpoint
    85  }