agent: Support for raw key lookup. Fixes #150.

This commit is contained in:
Armon Dadgar 2014-05-20 16:53:43 -07:00
parent fa4cb49feb
commit 88d91c6ee0
3 changed files with 48 additions and 0 deletions

View File

@ -76,6 +76,16 @@ func (s *HTTPServer) KVSGet(resp http.ResponseWriter, req *http.Request, args *s
resp.WriteHeader(404)
return nil, nil
}
// Check if we are in raw mode with a normal get, write out
// the raw body
if _, ok := params["raw"]; ok && method == "KVS.Get" {
body := out.Entries[0].Value
resp.Header().Set("Content-Length", strconv.FormatInt(int64(len(body)), 10))
resp.Write(body)
return nil, nil
}
return out.Entries, nil
}

View File

@ -409,3 +409,37 @@ func TestKVSEndpoint_AcquireRelease(t *testing.T) {
}
})
}
func TestKVSEndpoint_GET_Raw(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
buf := bytes.NewBuffer([]byte("test"))
req, err := http.NewRequest("PUT", "/v1/kv/test", buf)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := httptest.NewRecorder()
obj, err := srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if res := obj.(bool); !res {
t.Fatalf("should work")
}
req, err = http.NewRequest("GET", "/v1/kv/test?raw", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
resp = httptest.NewRecorder()
obj, err = srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
assertIndex(t, resp)
// Check the body
if !bytes.Equal(resp.Body.Bytes(), []byte("test")) {
t.Fatalf("bad: %s", resp.Body.Bytes())
}
})
}

View File

@ -145,6 +145,10 @@ For example, listing "/web/" with a "/" seperator may return:
Using the key listing method may be suitable when you do not need
the values or flags, or want to implement a key-space explorer.
If the "?raw" query parameter is used with a non-recursive GET,
then the response is just the raw value of the key, without any
encoding.
If no entries are found, a 404 code is returned.
This endpoint supports blocking queries and all consistency modes.