github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/registry/regserver/handlers/search_test.go (about)

     1  package handlers
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/qri-io/qri/registry"
    13  )
    14  
    15  func TestSearch(t *testing.T) {
    16  	reg := registry.Registry{
    17  		Profiles: registry.NewMemProfiles(),
    18  		Search:   &registry.MockSearch{},
    19  	}
    20  	s := httptest.NewServer(NewRoutes(reg))
    21  
    22  	cases := []struct {
    23  		method      string
    24  		endpoint    string
    25  		contentType string
    26  		params      *registry.SearchParams
    27  		resStatus   int
    28  	}{
    29  		// TODO (b5) - restore
    30  		// {"GET", "/registry/search", "application/json", &registry.SearchParams{Q: "abc", Limit: 0, Offset: 100}, 400},
    31  	}
    32  
    33  	for i, c := range cases {
    34  		req, err := http.NewRequest(c.method, fmt.Sprintf("%s%s", s.URL, c.endpoint), nil)
    35  		if err != nil {
    36  			t.Errorf("case %d error creating request: %s", i, err.Error())
    37  			continue
    38  		}
    39  		if c.contentType != "" {
    40  			req.Header.Set("Content-Type", c.contentType)
    41  		}
    42  		if c.params != nil {
    43  			data, err := json.Marshal(c.params)
    44  			if err != nil {
    45  				t.Errorf("error marshaling json body: %s", err.Error())
    46  				return
    47  			}
    48  			req.Body = ioutil.NopCloser(bytes.NewReader([]byte(data)))
    49  		}
    50  
    51  		res, err := http.DefaultClient.Do(req)
    52  		if err != nil {
    53  			t.Errorf("case %d unexpected error: %s", i, err)
    54  			continue
    55  		}
    56  
    57  		if res.StatusCode != c.resStatus {
    58  			t.Errorf("case %d res status mismatch. expected: %d, got: %d", i, c.resStatus, res.StatusCode)
    59  			continue
    60  		}
    61  	}
    62  }