Skip to content

Commit 291296b

Browse files
committed
fix(get): Get returns 404 if dataset is not found
1 parent fa71775 commit 291296b

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

api/api_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func init() {
3131
}
3232

3333
func newTestRepo(t *testing.T) (r repo.Repo, teardown func()) {
34+
return newTestRepoWithNumDatasets(t, 0)
35+
}
36+
37+
func newTestRepoWithNumDatasets(t *testing.T, num int) (r repo.Repo, teardown func()) {
3438
var err error
3539
if err = confirmQriNotRunning(); err != nil {
3640
t.Fatal(err.Error())
@@ -40,7 +44,7 @@ func newTestRepo(t *testing.T) (r repo.Repo, teardown func()) {
4044
golog.SetLogLevel("qriapi", "error")
4145

4246
// use a test registry server (with a pinset) & client & client
43-
rc, registryServer := regmock.NewMockServer()
47+
rc, registryServer := regmock.NewMockServerWithNumDatasets(num)
4448
// to keep hashes consistent, artificially specify the timestamp by overriding
4549
// the dsfs.Timestamp func
4650
prevTs := dsfs.Timestamp
@@ -74,8 +78,12 @@ func newTestRepo(t *testing.T) (r repo.Repo, teardown func()) {
7478
}
7579

7680
func newTestNode(t *testing.T) (node *p2p.QriNode, teardown func()) {
81+
return newTestNodeWithNumDatasets(t, 0)
82+
}
83+
84+
func newTestNodeWithNumDatasets(t *testing.T, num int) (node *p2p.QriNode, teardown func()) {
7785
var r repo.Repo
78-
r, teardown = newTestRepo(t)
86+
r, teardown = newTestRepoWithNumDatasets(t, num)
7987
node, err := p2p.NewQriNode(r, config.DefaultP2PForTesting())
8088
if err != nil {
8189
t.Fatal(err.Error())

api/registry.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67

78
util "github.com/datatogether/api/apiutil"
89
"github.com/qri-io/qri/lib"
@@ -131,6 +132,11 @@ func (h *RegistryHandlers) registryDatasetHandler(w http.ResponseWriter, r *http
131132

132133
err = h.RegistryRequests.GetDataset(&ref, res)
133134
if err != nil {
135+
// If error was that the dataset wasn't found, send back 404 Not Found.
136+
if strings.HasPrefix(err.Error(), "error 404") {
137+
util.NotFoundHandler(w, r)
138+
return
139+
}
134140
util.WriteErrResponse(w, http.StatusInternalServerError, err)
135141
return
136142
}

api/registry_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package api
22

33
import (
4+
"io/ioutil"
5+
"net/http/httptest"
6+
"strings"
7+
48
"testing"
59
)
610

@@ -23,3 +27,49 @@ func TestRegistryHandlers(t *testing.T) {
2327
}
2428
runHandlerTestCases(t, "registryDatasets", h.RegistryDatasetsHandler, registryDatasetsCases)
2529
}
30+
31+
32+
func TestRegistryGet(t *testing.T) {
33+
node, teardown := newTestNodeWithNumDatasets(t, 1)
34+
defer teardown()
35+
h := NewRegistryHandlers(node)
36+
37+
req := httptest.NewRequest("GET", "/registry/me/ds_0", strings.NewReader(""))
38+
w := httptest.NewRecorder()
39+
h.RegistryDatasetHandler(w, req)
40+
res := w.Result()
41+
42+
body, err := ioutil.ReadAll(res.Body)
43+
if err != nil {
44+
t.Fatalf(err.Error())
45+
}
46+
got := string(body)
47+
48+
expect := `{"data":{"peername":"peer","name":"ds_0","path":"QmAbC0","dataset":{"name":"ds_0","path":"QmAbC0","peername":"peer","qri":"ds:0"},"published":true},"meta":{"code":200}}`
49+
if got != expect {
50+
t.Errorf("did not match, got:\n%s\nexpect:\n%s\n", got, expect)
51+
}
52+
}
53+
54+
55+
func TestRegistryGetNotFound(t *testing.T) {
56+
node, teardown := newTestNodeWithNumDatasets(t, 1)
57+
defer teardown()
58+
h := NewRegistryHandlers(node)
59+
60+
req := httptest.NewRequest("GET", "/registry/me/not_found", strings.NewReader(""))
61+
w := httptest.NewRecorder()
62+
h.RegistryDatasetHandler(w, req)
63+
res := w.Result()
64+
65+
body, err := ioutil.ReadAll(res.Body)
66+
if err != nil {
67+
t.Fatalf(err.Error())
68+
}
69+
got := string(body)
70+
71+
expect := `{ "meta": { "code": 404, "status": "not found" }, "data": null }`
72+
if got != expect {
73+
t.Errorf("did not match, got:\n%s\nexpect:\n%s\n", got, expect)
74+
}
75+
}

0 commit comments

Comments
 (0)