Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #235 from suzuki-shunsuke/test/replace-gock-to-flute
Browse files Browse the repository at this point in the history
test: replace gock to flute
  • Loading branch information
suzuki-shunsuke committed Jan 18, 2020
2 parents 654c6fb + ad01a8d commit 41af4cc
Show file tree
Hide file tree
Showing 10 changed files with 973 additions and 413 deletions.
130 changes: 72 additions & 58 deletions client/dashboard_widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"

"gopkg.in/h2non/gock.v1"

"github.com/stretchr/testify/require"
"github.com/suzuki-shunsuke/flute/flute"
"github.com/suzuki-shunsuke/go-ptr"
Expand All @@ -21,66 +18,83 @@ import (

func TestClient_CreateDashboardWidget(t *testing.T) {
ctx := context.Background()
authName := os.Getenv("GRAYLOG_AUTH_NAME")
authPass := os.Getenv("GRAYLOG_AUTH_PASSWORD")
endpoint := os.Getenv("GRAYLOG_WEB_ENDPOINT_URI")
cl, err := client.NewClient("http://example.com/api", "admin", "admin")
require.Nil(t, err)

dashboardID := "5b65868b08813b0001777af3"

if endpoint == "" {
defer gock.Off()
endpoint = "http://example.com/api"
client, err := client.NewClient(endpoint, authName, authPass)
require.Nil(t, err)
data := []struct {
statusCode int
body interface{}
resp string
widget graylog.Widget
checkErr func(require.TestingT, interface{}, ...interface{})
}{{
body: map[string]interface{}{
"description": "Stream search result count",
"type": "STREAM_SEARCH_RESULT_COUNT",
"cache_time": 10,
"config": map[string]interface{}{
"timerange": map[string]interface{}{
"type": "relative",
"range": 300,
},
"query": "",
"lower_is_better": true,
"trend": true,
"stream_id": "000000000000000000000001",
}},
statusCode: 201,
resp: `{"widget_id": "ee2532ce-6995-4b8b-8c2c-4de327c6cce4"}`,
widget: graylog.Widget{
Description: "Stream search result count",
Config: &graylog.WidgetConfigStreamSearchResultCount{
Timerange: &graylog.Timerange{
Type: "relative",
Range: 300,
data := []struct {
statusCode int
body interface{}
resp string
widget graylog.Widget
checkErr func(require.TestingT, interface{}, ...interface{})
}{{
body: map[string]interface{}{
"description": "Stream search result count",
"type": "STREAM_SEARCH_RESULT_COUNT",
"cache_time": 10,
"config": map[string]interface{}{
"timerange": map[string]interface{}{
"type": "relative",
"range": 300,
},
"query": "",
"lower_is_better": true,
"trend": true,
"stream_id": "000000000000000000000001",
}},
statusCode: 201,
resp: `{"widget_id": "ee2532ce-6995-4b8b-8c2c-4de327c6cce4"}`,
widget: graylog.Widget{
Description: "Stream search result count",
Config: &graylog.WidgetConfigStreamSearchResultCount{
Timerange: &graylog.Timerange{
Type: "relative",
Range: 300,
},
LowerIsBetter: true,
Trend: true,
StreamID: "000000000000000000000001",
},
CacheTime: ptr.PInt(10),
},
checkErr: require.Nil,
}}
for _, d := range data {
cl.SetHTTPClient(&http.Client{
Transport: &flute.Transport{
T: t,
Services: []flute.Service{
{
Endpoint: "http://example.com",
Routes: []flute.Route{
{
Tester: &flute.Tester{
Method: "POST",
Path: fmt.Sprintf("/api/dashboards/%s/widgets", dashboardID),
PartOfHeader: getTestHeader(),
BodyJSON: d.body,
},
Response: &flute.Response{
Base: http.Response{
StatusCode: d.statusCode,
},
BodyString: d.resp,
},
},
},
},
LowerIsBetter: true,
Trend: true,
StreamID: "000000000000000000000001",
},
CacheTime: ptr.PInt(10),
},
checkErr: require.Nil,
}}
for _, d := range data {
gock.New("http://example.com").
Post(fmt.Sprintf("/api/dashboards/%s/widgets", dashboardID)).
MatchType("json").JSON(d.body).Reply(d.statusCode).
BodyString(d.resp)
w, _, err := client.CreateDashboardWidget(ctx, dashboardID, d.widget)
d.checkErr(t, err)
if err == nil {
require.NotEqual(t, "", w.ID)
d.widget.ID = w.ID
require.Equal(t, d.widget, w)
}
})

w, _, err := cl.CreateDashboardWidget(ctx, dashboardID, d.widget)
d.checkErr(t, err)
if err == nil {
require.NotEqual(t, "", w.ID)
d.widget.ID = w.ID
require.Equal(t, d.widget, w)
}
}
}
Expand Down

0 comments on commit 41af4cc

Please sign in to comment.