-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwriter_test.go
More file actions
61 lines (48 loc) · 1.3 KB
/
writer_test.go
File metadata and controls
61 lines (48 loc) · 1.3 KB
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
package goro_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/dghubble/sling"
"github.com/stretchr/testify/assert"
"github.com/vectorhacker/goro"
"github.com/gorilla/pat"
)
func TestWriter(t *testing.T) {
t.Run("it should write successfully", func(t *testing.T) {
d1 := []byte("{\"key\":\"value\"}")
evnt1 := goro.Event{
Data: d1,
Type: "testevent",
Version: 0,
ID: goro.NewUUID(),
}
evnt2 := goro.Event{
Data: d1,
Type: "testevent",
Version: 1,
ID: goro.NewUUID(),
}
mux := pat.New()
mux.Post("/streams/{stream}", func(w http.ResponseWriter, r *http.Request) {
stream := r.URL.Query().Get(":stream")
assert.Equal(t, "test", stream)
contentType := r.Header.Get("Content-Type")
assert.Equal(t, "application/vnd.eventstore.events+json", contentType)
events := goro.Events{}
err := json.NewDecoder(r.Body).Decode(&events)
assert.Nil(t, err)
assert.Len(t, events, 2)
w.WriteHeader(http.StatusCreated)
})
s := httptest.NewServer(mux)
w := goro.NewWriter(goro.SlingerFunc(func() *sling.Sling {
return sling.New().Base(s.URL).Client(s.Client())
}), "test")
ctx := context.Background()
err := w.Write(ctx, goro.ExpectedVersionAny, evnt1, evnt2)
assert.Nil(t, err)
})
}