github.com/prebid/prebid-server/v2@v2.18.0/analytics/pubstack/eventchannel/sender_test.go (about)

     1  package eventchannel
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestBuildEndpointSender(t *testing.T) {
    13  	requestBody := make([]byte, 10)
    14  	server := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
    15  		defer req.Body.Close()
    16  		requestBody, _ = io.ReadAll(req.Body)
    17  		res.WriteHeader(200)
    18  	}))
    19  
    20  	defer server.Close()
    21  
    22  	sender := BuildEndpointSender(server.Client(), server.URL, "module")
    23  	err := sender([]byte("message"))
    24  
    25  	assert.Equal(t, []byte("message"), requestBody)
    26  	assert.NoError(t, err)
    27  }
    28  
    29  func TestBuildEndpointSender_Error(t *testing.T) {
    30  	server := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
    31  		res.WriteHeader(400)
    32  	}))
    33  
    34  	defer server.Close()
    35  
    36  	sender := BuildEndpointSender(server.Client(), server.URL, "module")
    37  	err := sender([]byte("message"))
    38  
    39  	assert.Error(t, err)
    40  }