mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcdserver/etcdhttp: basic test working
This commit is contained in:
parent
7469871d20
commit
17e56a1c57
@ -13,8 +13,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"crypto/rand"
|
||||
"code.google.com/p/go.net/context"
|
||||
"crypto/rand"
|
||||
"github.com/coreos/etcd/elog"
|
||||
etcdserver "github.com/coreos/etcd/etcdserver2"
|
||||
"github.com/coreos/etcd/etcdserver2/etcdserverpb"
|
||||
@ -53,15 +53,22 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h Handler) serveKeys(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
rr := parseRequest(r)
|
||||
rr, err := parseRequest(r)
|
||||
if err != nil {
|
||||
log.Println(err) // reading of body failed
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.Server.Do(ctx, rr)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, "Internal Server Error", 500)
|
||||
return
|
||||
}
|
||||
|
||||
if err := encodeResponse(ctx, w, resp); err != nil {
|
||||
http.Error(w, "Timeout while waiting for response", 504)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,13 +94,17 @@ func genId() int64 {
|
||||
return int64(binary.BigEndian.Uint64(b))
|
||||
}
|
||||
|
||||
func parseRequest(r *http.Request) etcdserverpb.Request {
|
||||
func parseRequest(r *http.Request) (etcdserverpb.Request, error) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return etcdserverpb.Request{}, err
|
||||
}
|
||||
|
||||
q := r.URL.Query()
|
||||
rr := etcdserverpb.Request{
|
||||
Id: genId(),
|
||||
Method: r.Method,
|
||||
Val: r.FormValue("value"),
|
||||
Path: r.URL.Path[len("/keys"):],
|
||||
Val: q.Get("value"),
|
||||
PrevValue: q.Get("prevValue"),
|
||||
PrevIndex: parseUint64(q.Get("prevIndex")),
|
||||
Recursive: parseBool(q.Get("recursive")),
|
||||
@ -116,7 +127,7 @@ func parseRequest(r *http.Request) etcdserverpb.Request {
|
||||
rr.Expiration = time.Now().Add(expr).UnixNano()
|
||||
}
|
||||
|
||||
return rr
|
||||
return rr, nil
|
||||
}
|
||||
|
||||
func parseBool(s string) bool {
|
||||
|
@ -1,15 +1,14 @@
|
||||
package etcdhttp
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.net/context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
"code.google.com/p/go.net/context"
|
||||
|
||||
etcdserver "github.com/coreos/etcd/etcdserver2"
|
||||
"github.com/coreos/etcd/etcdserver2/etcdserverpb"
|
||||
@ -40,7 +39,6 @@ func TestSet(t *testing.T) {
|
||||
if err := r.Unmarshal(e.Data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Printf("r.Path: %q\n", r.Path)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user