Example #1
0
func TestHandler(t *testing.T) {
	for testn, tt := range handlerTests {
		fakeIndex := test.NewFakeIndex()
		idx := tt.setup(fakeIndex)

		indexOwner := owner
		if io, ok := idx.(indexOwnerer); ok {
			indexOwner = io.IndexOwner()
		}
		h := NewHandler(idx, indexOwner)

		req, err := http.NewRequest("GET", "/camli/search/"+tt.query, nil)
		if err != nil {
			t.Fatalf("%d: bad query: %v", testn, err)
		}
		req.Header.Set("X-PrefixHandler-PathSuffix", req.URL.Path[1:])

		rr := httptest.NewRecorder()
		rr.Body = new(bytes.Buffer)

		h.ServeHTTP(rr, req)

		got := rr.Body.Bytes()
		want, _ := json.MarshalIndent(tt.want, "", "  ")
		want = append(want, '\n')
		if !bytes.Equal(got, want) {
			t.Errorf("test %d:\nwant: %s\n got: %s", testn, want, got)
		}
	}
}
Example #2
0
func setupContent(owner blob.Ref, rootName string) *test.FakeIndex {

	picNode := blob.MustParse("picpn-1234")
	galRef := blob.MustParse("gal-1234")
	rootRef := blob.MustParse("root-abcd")
	camp0 := blob.MustParse("picpn-9876543210")
	camp1 := blob.MustParse("picpn-9876543211")
	camp0f := blob.MustParse("picfile-f00ff00f00a5")
	camp1f := blob.MustParse("picfile-f00ff00f00b6")

	idx := test.NewFakeIndex()
	idx.AddSignerAttrValue(owner, "camliRoot", rootName, rootRef)

	idx.AddMeta(owner, "", 100)
	for _, br := range []blob.Ref{picNode, galRef, rootRef, camp0, camp1} {
		idx.AddMeta(br, "permanode", 100)
	}
	for _, br := range []blob.Ref{camp0f, camp1f} {
		idx.AddMeta(br, "file", 100)
	}

	idx.AddClaim(owner, rootRef, "set-attribute", "camliPath:singlepic", picNode.String())
	idx.AddClaim(owner, rootRef, "set-attribute", "camliPath:camping", galRef.String())
	idx.AddClaim(owner, galRef, "add-attribute", "camliMember", camp0.String())
	idx.AddClaim(owner, galRef, "add-attribute", "camliMember", camp1.String())
	idx.AddClaim(owner, camp0, "set-attribute", "camliContent", camp0f.String())
	idx.AddClaim(owner, camp1, "set-attribute", "camliContent", camp1f.String())

	return idx
}
Example #3
0
func TestDescribeLocation(t *testing.T) {
	tests := []struct {
		ref       string
		lat, long float64
		hasNoLoc  bool
	}{
		{ref: "filewithloc-0", lat: 45, long: 56},
		{ref: "location-0", lat: 45, long: 56},
		{ref: "locationpriority-1", lat: 67, long: 78},
		{ref: "locationpriority-2", lat: 12, long: 34},
		{ref: "locationoverride-1", lat: 67, long: 78},
		{ref: "locationoverride-2", lat: 67, long: 78},
		{ref: "homedir-0", hasNoLoc: true},
	}

	ix := searchDescribeSetup(test.NewFakeIndex())
	ctx := context.Background()
	h := search.NewHandler(ix, owner)

	ix.RLock()
	defer ix.RUnlock()

	for _, tt := range tests {
		var err error
		br := blob.MustParse(tt.ref)
		res, err := h.Describe(ctx, &search.DescribeRequest{
			BlobRef: br,
			Depth:   1,
		})
		if err != nil {
			t.Errorf("Describe for %v failed: %v", br, err)
			continue
		}
		db := res.Meta[br.String()]
		if db == nil {
			t.Errorf("Describe result for %v is missing", br)
			continue
		}
		loc := db.Location
		if tt.hasNoLoc {
			if loc != nil {
				t.Errorf("got location for %v, should have no location", br)
			}
		} else {
			if loc == nil {
				t.Errorf("no location in result for %v", br)
				continue
			}
			if loc.Latitude != tt.lat || loc.Longitude != tt.long {
				t.Errorf("location for %v invalid, got %f,%f want %f,%f",
					tt.ref, loc.Latitude, loc.Longitude, tt.lat, tt.long)
			}
		}
	}
}
Example #4
0
func TestHandler(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
		return
	}
	seen := map[string]bool{}
	defer SetTestHookBug121(func() {})
	for _, tt := range handlerTests {
		if seen[tt.name] {
			t.Fatalf("duplicate test named %q", tt.name)
		}
		seen[tt.name] = true
		SetTestHookBug121(func() {})

		fakeIndex := test.NewFakeIndex()
		idx := tt.setup(fakeIndex)

		indexOwner := owner
		if io, ok := idx.(indexOwnerer); ok {
			indexOwner = io.IndexOwner()
		}
		h := NewHandler(idx, indexOwner)

		req, err := http.NewRequest("GET", "/camli/search/"+tt.query, nil)
		if err != nil {
			t.Fatalf("%s: bad query: %v", tt.name, err)
		}
		req.Header.Set(httputil.PathSuffixHeader, req.URL.Path[1:])

		rr := httptest.NewRecorder()
		rr.Body = new(bytes.Buffer)

		h.ServeHTTP(rr, req)

		got := rr.Body.Bytes()
		want, _ := json.MarshalIndent(tt.want, "", "  ")
		trim := bytes.TrimSpace

		if bytes.Equal(trim(got), trim(want)) {
			continue
		}

		// Try with re-encoded got, since the JSON ordering doesn't matter
		// to the test,
		gotj := parseJSON(string(got))
		got2, _ := json.MarshalIndent(gotj, "", "  ")
		if bytes.Equal(got2, want) {
			continue
		}
		diff := test.Diff(want, got2)

		t.Errorf("test %s:\nwant: %s\n got: %s\ndiff:\n%s", tt.name, want, got, diff)
	}
}
Example #5
0
func TestDescribe(t *testing.T) {
	for testn, tt := range describeTests {
		idx := test.NewFakeIndex()
		tt.setup(idx)

		h := NewHandler(idx, owner)
		js := make(map[string]interface{})
		dr := h.NewDescribeRequest()
		dr.Describe(blobref.MustParse(tt.blob), tt.depth)
		dr.PopulateJSON(js)
		got, _ := json.MarshalIndent(js, "", "  ")
		want, _ := json.MarshalIndent(tt.expect, "", "  ")
		if !bytes.Equal(got, want) {
			t.Errorf("test %d:\nwant: %s\n got: %s", testn, string(want), string(got))
		}
	}
}
Example #6
0
func TestPublishURLs(t *testing.T) {
	owner := blobref.MustParse("owner-123")
	picNode := blobref.MustParse("picpn-123")
	galRef := blobref.MustParse("gal-123")
	rootRef := blobref.MustParse("root-abc")
	camp0 := blobref.MustParse("picpn-98765432100")
	camp1 := blobref.MustParse("picpn-98765432111")
	camp0f := blobref.MustParse("picfile-f00f00f00a5")
	camp1f := blobref.MustParse("picfile-f00f00f00b6")

	rootName := "foo"

	for ti, tt := range publishURLTests {
		idx := test.NewFakeIndex()
		idx.AddSignerAttrValue(owner, "camliRoot", rootName, rootRef)
		sh := search.NewHandler(idx, owner)
		ph := &PublishHandler{
			RootName: rootName,
			Search:   sh,
		}

		idx.AddMeta(owner, "text/x-openpgp-public-key", 100)
		for _, br := range []*blobref.BlobRef{picNode, galRef, rootRef, camp0, camp1} {
			idx.AddMeta(br, "application/json; camliType=permanode", 100)
		}
		for _, br := range []*blobref.BlobRef{camp0f, camp1f} {
			idx.AddMeta(br, "application/json; camliType=file", 100)
		}

		idx.AddClaim(owner, rootRef, "set-attribute", "camliPath:singlepic", picNode.String())
		idx.AddClaim(owner, rootRef, "set-attribute", "camliPath:camping", galRef.String())
		idx.AddClaim(owner, galRef, "add-attribute", "camliMember", camp0.String())
		idx.AddClaim(owner, galRef, "add-attribute", "camliMember", camp1.String())
		idx.AddClaim(owner, camp0, "set-attribute", "camliContent", camp0f.String())
		idx.AddClaim(owner, camp1, "set-attribute", "camliContent", camp1f.String())

		rw := httptest.NewRecorder()
		if !strings.HasPrefix(tt.path, "/pics/") {
			panic("expected /pics/ prefix on " + tt.path)
		}
		req, _ := http.NewRequest("GET", "http://foo.com"+tt.path, nil)

		pfxh := &httputil.PrefixHandler{
			Prefix: "/pics/",
			Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
				pr := ph.NewRequest(rw, req)

				err := pr.findSubject()
				if tt.subject != "" {
					if err != nil {
						t.Errorf("test #%d, findSubject: %v", ti, err)
						return
					}
					if pr.subject.String() != tt.subject {
						t.Errorf("test #%d, got subject %q, want %q", ti, pr.subject, tt.subject)
					}
				}
				if pr.subres != tt.subres {
					t.Errorf("test #%d, got subres %q, want %q", ti, pr.subres, tt.subres)
				}
			}),
		}
		pfxh.ServeHTTP(rw, req)
	}
}
func (ht handlerTest) test(t *testing.T) {
	SetTestHookBug121(func() {})

	fakeIndex := test.NewFakeIndex()
	idx := ht.setup(fakeIndex)

	indexOwner := owner
	if io, ok := idx.(indexOwnerer); ok {
		indexOwner = io.IndexOwner()
	}
	h := NewHandler(idx, indexOwner)

	var body io.Reader
	var method = "GET"
	if ht.postBody != "" {
		method = "POST"
		body = strings.NewReader(ht.postBody)
	}
	req, err := http.NewRequest(method, "/camli/search/"+ht.query, body)
	if err != nil {
		t.Fatalf("%s: bad query: %v", ht.name, err)
	}
	req.Header.Set(httputil.PathSuffixHeader, req.URL.Path[1:])

	rr := httptest.NewRecorder()
	rr.Body = new(bytes.Buffer)

	h.ServeHTTP(rr, req)
	got := rr.Body.Bytes()

	if len(ht.wantDescribed) > 0 {
		dr := new(DescribeResponse)
		if err := json.NewDecoder(bytes.NewReader(got)).Decode(dr); err != nil {
			t.Fatalf("On test %s: Non-JSON response: %s", ht.name, got)
		}
		var gotDesc []string
		for k := range dr.Meta {
			gotDesc = append(gotDesc, k)
		}
		sort.Strings(ht.wantDescribed)
		sort.Strings(gotDesc)
		if !reflect.DeepEqual(gotDesc, ht.wantDescribed) {
			t.Errorf("On test %s: described blobs:\n%v\nwant:\n%v\n",
				ht.name, gotDesc, ht.wantDescribed)
		}
		if ht.want == nil {
			return
		}
	}

	want, _ := json.MarshalIndent(ht.want, "", "  ")
	trim := bytes.TrimSpace

	if bytes.Equal(trim(got), trim(want)) {
		return
	}

	// Try with re-encoded got, since the JSON ordering doesn't matter
	// to the test,
	gotj := parseJSON(string(got))
	got2, _ := json.MarshalIndent(gotj, "", "  ")
	if bytes.Equal(got2, want) {
		return
	}
	diff := test.Diff(want, got2)

	t.Errorf("test %s:\nwant: %s\n got: %s\ndiff:\n%s", ht.name, want, got, diff)
}