github.hscsec.cn/openshift/source-to-image@v1.2.0/pkg/util/callback_test.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"testing"
     9  )
    10  
    11  type FakePost struct {
    12  	err      error
    13  	response http.Response
    14  	body     []byte
    15  	url      string
    16  }
    17  
    18  func (f *FakePost) post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
    19  	f.url = url
    20  	f.body, _ = ioutil.ReadAll(body)
    21  	return &f.response, f.err
    22  }
    23  
    24  func TestExecuteCallback(t *testing.T) {
    25  	fp := FakePost{}
    26  	cb := callbackInvoker{
    27  		postFunc: fp.post,
    28  	}
    29  
    30  	labels := map[string]string{
    31  		"foo": "bar",
    32  	}
    33  	cb.ExecuteCallback("http://the.callback.url/test", true, labels, []string{"msg1", "msg2"})
    34  
    35  	type postBody struct {
    36  		Labels  map[string]string
    37  		Success bool
    38  	}
    39  	var pb postBody
    40  	json.Unmarshal(fp.body, &pb)
    41  	if len(pb.Labels) == 0 {
    42  		t.Errorf("Expected labels to be present in payload")
    43  	}
    44  	if pb.Labels["foo"] != "bar" {
    45  		t.Errorf("Expected 'foo' to be 'bar', got %q", pb.Labels["foo"])
    46  	}
    47  	if pb.Success != true {
    48  		t.Errorf("Unexpected success flag: %v", pb.Success)
    49  	}
    50  }