(t *testing.T)
| 873 | } |
| 874 | |
| 875 | func (ht handlerTest) test(t *testing.T) { |
| 876 | SetTestHookBug121(func() {}) |
| 877 | |
| 878 | ixo := ht.setup(t) |
| 879 | idx := ixo.index |
| 880 | h := NewHandler(idx, owner) |
| 881 | |
| 882 | var body io.Reader |
| 883 | var method = "GET" |
| 884 | if ht.postBody != "" { |
| 885 | method = "POST" |
| 886 | body = strings.NewReader(ht.postBody) |
| 887 | } |
| 888 | req, err := http.NewRequest(method, "/camli/search/"+ht.query, body) |
| 889 | if err != nil { |
| 890 | t.Fatalf("%s: bad query: %v", ht.name, err) |
| 891 | } |
| 892 | req.Header.Set(httputil.PathSuffixHeader, req.URL.Path[1:]) |
| 893 | |
| 894 | rr := httptest.NewRecorder() |
| 895 | rr.Body = new(bytes.Buffer) |
| 896 | |
| 897 | h.ServeHTTP(rr, req) |
| 898 | got := rr.Body.Bytes() |
| 899 | |
| 900 | if len(ht.wantDescribed) > 0 { |
| 901 | dr := new(DescribeResponse) |
| 902 | if err := json.NewDecoder(bytes.NewReader(got)).Decode(dr); err != nil { |
| 903 | t.Fatalf("On test %s: Non-JSON response: %s", ht.name, got) |
| 904 | } |
| 905 | var gotDesc []string |
| 906 | for k := range dr.Meta { |
| 907 | gotDesc = append(gotDesc, k) |
| 908 | } |
| 909 | sort.Strings(ht.wantDescribed) |
| 910 | sort.Strings(gotDesc) |
| 911 | if !reflect.DeepEqual(gotDesc, ht.wantDescribed) { |
| 912 | t.Errorf("On test %s: described blobs:\n%v\nwant:\n%v\n", |
| 913 | ht.name, gotDesc, ht.wantDescribed) |
| 914 | } |
| 915 | if ht.want == nil { |
| 916 | return |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | want, _ := json.MarshalIndent(ht.want, "", " ") |
| 921 | trim := bytes.TrimSpace |
| 922 | |
| 923 | if bytes.Equal(trim(got), trim(want)) { |
| 924 | return |
| 925 | } |
| 926 | |
| 927 | // Try with re-encoded got, since the JSON ordering doesn't matter |
| 928 | // to the test, |
| 929 | gotj := parseJSON(string(got)) |
| 930 | got2, _ := json.MarshalIndent(gotj, "", " ") |
| 931 | if bytes.Equal(got2, want) { |
| 932 | return |
no test coverage detected