This repository was archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathput_test.go
75 lines (61 loc) · 1.41 KB
/
put_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package w3s
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/ipld/go-car"
)
func hasValidToken(w http.ResponseWriter, r *http.Request) bool {
auth := r.Header.Get("Authorization")
if auth != "Bearer "+validToken {
w.WriteHeader(http.StatusUnauthorized)
return false
}
return true
}
var putCarHandler = func(w http.ResponseWriter, r *http.Request) {
if !hasValidToken(w, r) {
return
}
cr, err := car.NewCarReader(r.Body)
if err != nil {
fmt.Printf("NewCarReader: %v\n", err)
w.WriteHeader(http.StatusBadRequest)
}
rootCid := cr.Header.Roots[0]
w.WriteHeader(http.StatusOK)
var out struct {
Cid string `json:"cid"`
}
out.Cid = rootCid.String()
json.NewEncoder(w).Encode(out)
}
func TestPutCarHappyPath(t *testing.T) {
routes := routeMap{
"/car": {
http.MethodPost: putCarHandler,
},
}
hc, cleanup := startTestServer(t, routes)
defer cleanup()
client, err := NewClient(WithHTTPClient(hc), WithToken("validtoken"))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
carbytes, err := hex.DecodeString(helloCarHex)
if err != nil {
t.Fatalf("failed to decode car hex: %v", err)
return
}
c, err := client.PutCar(context.Background(), bytes.NewReader(carbytes))
if err != nil {
t.Fatalf("failed to send request: %v", err)
}
if c.String() != helloRoot {
t.Fatalf("got cid %s, wanted %s", c.String(), helloRoot)
}
}