github.com/Axway/agent-sdk@v1.1.101/pkg/api/mockhttpclient.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"os"
     8  	"sync"
     9  
    10  	log "github.com/Axway/agent-sdk/pkg/util/log"
    11  )
    12  
    13  // MockHTTPClient - use for mocking the HTTP client
    14  type MockHTTPClient struct {
    15  	Client
    16  	Response      *Response // this for if you want to set your own dummy response
    17  	ResponseCode  int       // this for if you only care about a particular response code
    18  	ResponseError error
    19  
    20  	RespCount int
    21  	Responses []MockResponse
    22  	Requests  []Request // lists all requests the client has received
    23  	sync.Mutex
    24  }
    25  
    26  // MockResponse - use for mocking the MockHTTPClient responses
    27  type MockResponse struct {
    28  	FileName  string
    29  	RespData  string
    30  	RespCode  int
    31  	ErrString string
    32  }
    33  
    34  // SetResponse -
    35  // if you care about the response content and the code, pass both in
    36  // if you only care about the code, pass "" for the filepath
    37  func (c *MockHTTPClient) SetResponse(filepath string, code int) {
    38  	var dat []byte
    39  	if filepath != "" {
    40  		responseFile, err := os.Open(filepath)
    41  		if err != nil {
    42  			c.ResponseCode = http.StatusInternalServerError
    43  			return
    44  		}
    45  		dat, err = io.ReadAll(responseFile)
    46  		if err != nil {
    47  			c.ResponseCode = http.StatusInternalServerError
    48  			return
    49  		}
    50  	}
    51  
    52  	c.Response = &Response{
    53  		Code:    code,
    54  		Body:    dat,
    55  		Headers: map[string][]string{},
    56  	}
    57  }
    58  
    59  // SetResponses -
    60  // if you care about the response content and the code, pass both in
    61  // if you only care about the code, pass "" for the filepath
    62  func (c *MockHTTPClient) SetResponses(responses []MockResponse) {
    63  	c.Lock()
    64  	defer c.Unlock()
    65  	c.RespCount = 0
    66  	c.Responses = responses
    67  }
    68  
    69  // Send -
    70  func (c *MockHTTPClient) Send(request Request) (*Response, error) {
    71  	c.Lock()
    72  	defer c.Unlock()
    73  
    74  	c.Requests = append(c.Requests, request)
    75  
    76  	fmt.Printf("%v - %v\n", request.Method, request.URL)
    77  
    78  	if c.Responses != nil && len(c.Responses) > 0 {
    79  		return c.sendMultiple(request)
    80  	}
    81  	if c.Response != nil {
    82  		return c.Response, nil
    83  	}
    84  	if c.ResponseError != nil {
    85  		return nil, c.ResponseError
    86  	}
    87  	if c.ResponseCode != 0 {
    88  		return &Response{
    89  			Code: c.ResponseCode,
    90  		}, nil
    91  	}
    92  	return nil, nil
    93  }
    94  
    95  func (c *MockHTTPClient) sendMultiple(request Request) (*Response, error) {
    96  	var err error
    97  	if c.RespCount >= len(c.Responses) {
    98  		err := fmt.Errorf("error: received more requests than saved responses. failed on request: %s", request.URL)
    99  		log.Error(err)
   100  		return nil, err
   101  	}
   102  
   103  	fileName := c.Responses[c.RespCount].FileName
   104  
   105  	dat := []byte(c.Responses[c.RespCount].RespData)
   106  
   107  	var responseFile *os.File
   108  
   109  	if fileName != "" {
   110  		responseFile, err = os.Open(fileName)
   111  		if err != nil {
   112  			return nil, err
   113  		}
   114  
   115  		dat, err = io.ReadAll(responseFile)
   116  		if err != nil {
   117  			return nil, err
   118  		}
   119  	}
   120  
   121  	response := Response{
   122  		Code:    c.Responses[c.RespCount].RespCode,
   123  		Body:    dat,
   124  		Headers: map[string][]string{},
   125  	}
   126  
   127  	if c.Responses[c.RespCount].ErrString != "" {
   128  		err = fmt.Errorf(c.Responses[c.RespCount].ErrString)
   129  	}
   130  	c.RespCount++
   131  	return &response, err
   132  }