|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + dapr "github.com/mchmarny/godapr/v1" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "go.opencensus.io/trace" |
| 15 | +) |
| 16 | + |
| 17 | +func TestDefaultHandler(t *testing.T) { |
| 18 | + daprClient = GetTestClient() |
| 19 | + gin.SetMode(gin.DebugMode) |
| 20 | + |
| 21 | + r := gin.New() |
| 22 | + r.Use(gin.Recovery()) |
| 23 | + r.Use(Options) |
| 24 | + r.GET("/", defaultHandler) |
| 25 | + w := httptest.NewRecorder() |
| 26 | + |
| 27 | + req, _ := http.NewRequest("GET", "/", nil) |
| 28 | + req.Header.Set("Content-Type", "application/json") |
| 29 | + |
| 30 | + r.ServeHTTP(w, req) |
| 31 | + assert.Equal(t, http.StatusOK, w.Code) |
| 32 | +} |
| 33 | + |
| 34 | +func TestSubscriptionHandler(t *testing.T) { |
| 35 | + daprClient = GetTestClient() |
| 36 | + gin.SetMode(gin.DebugMode) |
| 37 | + |
| 38 | + r := gin.New() |
| 39 | + r.Use(gin.Recovery()) |
| 40 | + r.Use(Options) |
| 41 | + r.GET("/", subscriptionHandler) |
| 42 | + w := httptest.NewRecorder() |
| 43 | + |
| 44 | + req, _ := http.NewRequest("GET", "/", nil) |
| 45 | + req.Header.Set("Content-Type", "application/json") |
| 46 | + |
| 47 | + r.ServeHTTP(w, req) |
| 48 | + assert.Equal(t, http.StatusOK, w.Code) |
| 49 | + |
| 50 | + content, err := ioutil.ReadAll(w.Body) |
| 51 | + assert.Nil(t, err) |
| 52 | + |
| 53 | + var subs []dapr.Subscription |
| 54 | + err = json.Unmarshal(content, &subs) |
| 55 | + assert.Nil(t, err) |
| 56 | + assert.Lenf(t, subs, 1, "minimum 1 subscription required, got: %v", subs) |
| 57 | +} |
| 58 | + |
| 59 | +func TestEventHandler(t *testing.T) { |
| 60 | + daprClient = GetTestClient() |
| 61 | + gin.SetMode(gin.DebugMode) |
| 62 | + |
| 63 | + r := gin.New() |
| 64 | + r.Use(gin.Recovery()) |
| 65 | + r.Use(Options) |
| 66 | + r.POST("/", eventHandler) |
| 67 | + w := httptest.NewRecorder() |
| 68 | + |
| 69 | + data, err := ioutil.ReadFile("./event.json") |
| 70 | + assert.Nil(t, err) |
| 71 | + |
| 72 | + req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data)) |
| 73 | + req.Header.Set("Content-Type", "application/json") |
| 74 | + |
| 75 | + r.ServeHTTP(w, req) |
| 76 | + assert.Equal(t, http.StatusOK, w.Code) |
| 77 | +} |
| 78 | + |
| 79 | +func GetTestClient() *TestClient { |
| 80 | + return &TestClient{} |
| 81 | +} |
| 82 | + |
| 83 | +var ( |
| 84 | + // test test client against local interace |
| 85 | + _ = Client(&TestClient{}) |
| 86 | +) |
| 87 | + |
| 88 | +type TestClient struct { |
| 89 | +} |
| 90 | + |
| 91 | +func (c *TestClient) GetState(ctx trace.SpanContext, store, key string) ([]byte, error) { |
| 92 | + return []byte(`{ "message": "hello" }`), nil |
| 93 | +} |
| 94 | +func (c *TestClient) SaveState(ctx trace.SpanContext, store, key string, data interface{}) error { |
| 95 | + return nil |
| 96 | +} |
| 97 | +func (c *TestClient) DeleteState(ctx trace.SpanContext, store, key string) error { |
| 98 | + return nil |
| 99 | +} |
0 commit comments