github.com/landoop/schema-registry@v0.0.0-20190327143759-50a5701c1891/client_test.go (about)

     1  package schemaregistry
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"reflect"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  const testHost = "testhost:1337"
    14  const testURL = "http://" + testHost
    15  
    16  type D func(req *http.Request) (*http.Response, error)
    17  
    18  func (d D) Do(req *http.Request) (*http.Response, error) {
    19  	return d(req)
    20  }
    21  
    22  // verifies the http.Request, creates an http.Response
    23  func dummyHTTPHandler(t *testing.T, method, path string, status int, reqBody, respBody interface{}) D {
    24  	d := D(func(req *http.Request) (*http.Response, error) {
    25  		if method != "" && req.Method != method {
    26  			t.Errorf("method is wrong, expected `%s`, got `%s`", method, req.Method)
    27  		}
    28  		if req.URL.Host != testHost {
    29  			t.Errorf("expected host `%s`, got `%s`", testHost, req.URL.Host)
    30  		}
    31  		if path != "" && req.URL.Path != path {
    32  			t.Errorf("path is wrong, expected `%s`, got `%s`", path, req.URL.Path)
    33  		}
    34  		if reqBody != nil {
    35  			expbs, err := json.Marshal(reqBody)
    36  			if err != nil {
    37  				t.Error(err)
    38  			}
    39  			bs, err := ioutil.ReadAll(req.Body)
    40  			mustEqual(t, strings.Trim(string(bs), "\r\n"), strings.Trim(string(expbs), "\r\n"))
    41  		}
    42  		var resp http.Response
    43  		resp.Header = http.Header{contentTypeHeaderKey: []string{contentTypeJSON}}
    44  		resp.StatusCode = status
    45  		if respBody != nil {
    46  			bs, err := json.Marshal(respBody)
    47  			if err != nil {
    48  				t.Error(err)
    49  			}
    50  			resp.Body = ioutil.NopCloser(bytes.NewReader(bs))
    51  		}
    52  		return &resp, nil
    53  	})
    54  	return d
    55  }
    56  
    57  func httpSuccess(t *testing.T, method, path string, reqBody, respBody interface{}) *Client {
    58  	return &Client{testURL, dummyHTTPHandler(t, method, path, 200, reqBody, respBody)}
    59  }
    60  
    61  func httpError(t *testing.T, status, errCode int, errMsg string) *Client {
    62  	return &Client{testURL, dummyHTTPHandler(t, "", "", status, nil, ResourceError{ErrorCode: errCode, Message: errMsg})}
    63  }
    64  
    65  func mustEqual(t *testing.T, actual, expected interface{}) {
    66  	if !reflect.DeepEqual(actual, expected) {
    67  		t.Errorf("expected `%#v`, got `%#v`", expected, actual)
    68  	}
    69  }
    70  
    71  func TestSubjects(t *testing.T) {
    72  	subsIn := []string{"rollulus", "hello-subject"}
    73  	c := httpSuccess(t, "GET", "/subjects", nil, subsIn)
    74  	subs, err := c.Subjects()
    75  	if err != nil {
    76  		t.Error()
    77  	}
    78  	mustEqual(t, subs, subsIn)
    79  }
    80  
    81  func TestVersions(t *testing.T) {
    82  	versIn := []int{1, 2, 3}
    83  	c := httpSuccess(t, "GET", "/subjects/mysubject/versions", nil, versIn)
    84  	vers, err := c.Versions("mysubject")
    85  	if err != nil {
    86  		t.Error()
    87  	}
    88  	mustEqual(t, vers, versIn)
    89  }
    90  
    91  func TestIsRegistered_yes(t *testing.T) {
    92  	s := `{"x":"y"}`
    93  	ss := schemaOnlyJSON{s}
    94  	sIn := Schema{s, "mysubject", 4, 7}
    95  	c := httpSuccess(t, "POST", "/subjects/mysubject", ss, sIn)
    96  	isreg, sOut, err := c.IsRegistered("mysubject", s)
    97  	if err != nil {
    98  		t.Error()
    99  	}
   100  	if !isreg {
   101  		t.Error()
   102  	}
   103  	mustEqual(t, sOut, sIn)
   104  }
   105  
   106  func TestIsRegistered_not(t *testing.T) {
   107  	c := httpError(t, 404, schemaNotFoundCode, "too bad")
   108  	isreg, _, err := c.IsRegistered("mysubject", "{}")
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	if isreg {
   113  		t.Fatalf("is registered: %v", err)
   114  	}
   115  }