예제 #1
0
func TestParser(t *testing.T) {
	is := is.New(t)

	groups, err := parse.ParseFile("../testfiles/success/comments.silk.md", "../testfiles/success/comments2.silk.md")
	is.NoErr(err)

	is.Equal(len(groups), 3)
	is.Equal(groups[0].Filename, "../testfiles/success/comments.silk.md")
	is.Equal(groups[1].Filename, "../testfiles/success/comments.silk.md")
	is.Equal(groups[2].Filename, "../testfiles/success/comments2.silk.md")
	is.Equal(groups[0].Title, "Comments and things")
	is.Equal(groups[1].Title, "Another group")

	group := groups[0]
	is.Equal(len(group.Requests), 2)
	is.Equal(len(group.Details), 1)
	is.Equal(group.Details[0].Detail().Key, "Root")
	is.Equal(group.Details[0].Detail().Value.Data, "http://localhost:8080/")

	req1 := group.Requests[0]
	is.Equal("POST", string(req1.Method))
	is.Equal("/comments", string(req1.Path))
	is.Equal(len(req1.Details), 1)
	is.Equal(req1.Details[0].Detail().Key, "Content-Type")
	is.Equal(req1.Details[0].Detail().Value.Data, "application/json")
	is.Equal(req1.ExpectedDetails[0].Detail().Key, "Status")
	is.Equal(req1.ExpectedDetails[0].Detail().Value.Data, 201)
	is.Equal(req1.Body.String(), `{
  "name":    "Mat",
  "comment": "Good work"
}`)
	is.Equal(req1.BodyType, "json")
	is.Equal(req1.ExpectedBody.String(), `{
  "id":      "123",
  "name":    "Mat",
  "comment": "Good work"
}`)
	is.Equal(req1.ExpectedBodyType, "json")

	req2 := group.Requests[1]
	is.Equal("GET", req2.Method)
	is.Equal("/comments/{id}", req2.Path)
	is.Equal(len(req2.Params), 1)
	is.Equal(req2.Params[0].Detail().Key, "pretty")
	is.Equal(req2.Params[0].Detail().Value.Data, true)
	is.Equal(req2.ExpectedDetails[0].Detail().Key, "Status")
	is.Equal(req2.ExpectedDetails[0].Detail().Value.Data, 200)
	is.Equal(req2.ExpectedDetails[1].Detail().Key, "Content-Type")
	is.Equal(req2.ExpectedDetails[1].Detail().Value.Data, "application/json")
	is.Equal(req2.ExpectedBody.String(), `{
  "id":      "123",
  "name":    "Mat",
  "comment": "Good work"
}`)
	is.Equal(req2.ExpectedBody.Number(), 46)

	group = groups[1]
	is.Equal(len(group.Requests), 1)

}
예제 #2
0
파일: run.go 프로젝트: matryer/silk
// RunFile parses and runs the specified file(s).
func (r *Runner) RunFile(filenames ...string) {
	groups, err := parse.ParseFile(filenames...)
	if err != nil {
		r.log(err)
		return
	}
	r.RunGroup(groups...)
}
예제 #3
0
func TestRunJsonModesFailure(t *testing.T) {
	is := is.New(t)
	subT := &testT{}
	s := httptest.NewServer(testutil.EchoRawHandler())
	defer s.Close()
	r := runner.New(subT, s.URL)
	g, err := parse.ParseFile("../testfiles/failure/echoraw.failure.jsonmodes.silk.md")
	is.NoErr(err)
	r.RunGroup(g...)
	is.True(subT.Failed())
}
예제 #4
0
func TestRunGroupSuccess(t *testing.T) {
	is := is.New(t)
	subT := &testT{}
	s := httptest.NewServer(testutil.EchoHandler())
	defer s.Close()
	r := runner.New(subT, s.URL)
	g, err := parse.ParseFile("../testfiles/success/echo.success.silk.md")
	is.NoErr(err)
	r.RunGroup(g...)
	is.False(subT.Failed())
}
예제 #5
0
func TestFailureFieldsDifferentTypes(t *testing.T) {
	is := is.New(t)
	subT := &testT{}
	s := httptest.NewServer(testutil.EchoHandler())
	defer s.Close()
	r := runner.New(subT, s.URL)
	var logs []string
	r.Log = func(s string) {
		logs = append(logs, s)
	}
	g, err := parse.ParseFile("../testfiles/failure/echo.failure.fieldsdifferenttypes.silk.md")
	is.NoErr(err)
	r.RunGroup(g...)
	is.True(subT.Failed())
	logstr := strings.Join(logs, "\n")

	is.True(strings.Contains(logstr, `Status expected string: "400"  actual float64: 200`))
}
예제 #6
0
func TestFailureWrongHeader(t *testing.T) {
	is := is.New(t)
	subT := &testT{}
	s := httptest.NewServer(testutil.EchoHandler())
	defer s.Close()
	r := runner.New(subT, s.URL)
	var logs []string
	r.Log = func(s string) {
		logs = append(logs, s)
	}
	g, err := parse.ParseFile("../testfiles/failure/echo.failure.wrongheader.silk.md")
	is.NoErr(err)
	r.RunGroup(g...)
	is.True(subT.Failed())
	logstr := strings.Join(logs, "\n")
	is.True(strings.Contains(logstr, `Content-Type expected string: "wrong/type"  actual string: "text/plain; charset=utf-8"`))
	is.True(strings.Contains(logstr, "--- FAIL: GET /echo"))
	is.True(strings.Contains(logstr, "../testfiles/failure/echo.failure.wrongheader.silk.md:22 - Content-Type doesn't match"))
}
예제 #7
0
// https://github.com/matryer/silk/issues/28
func TestFailureNonTrimmedExpection(t *testing.T) {
	is := is.New(t)
	subT := &testT{}
	s := httptest.NewServer(testutil.EchoDataHandler())
	defer s.Close()
	r := runner.New(subT, s.URL)
	var logs []string
	r.Log = func(s string) {
		logs = append(logs, s)
	}
	g, err := parse.ParseFile("../testfiles/failure/echo.failure.nontrimmedexpectation.silk.md")
	is.NoErr(err)
	r.RunGroup(g...)
	is.True(subT.Failed())
	logstr := strings.Join(logs, "\n")

	is.True(strings.Contains(logstr, `Data.body.status expected: "awesome"  actual: " awesome"`))
	is.True(strings.Contains(logstr, "--- FAIL: GET /echo"))
	is.True(strings.Contains(logstr, "../testfiles/failure/echo.failure.nontrimmedexpectation.silk.md:18 - Data.body.status doesn't match"))
}
예제 #8
0
func TestFailureWrongBody(t *testing.T) {
	is := is.New(t)
	subT := &testT{}
	s := httptest.NewServer(testutil.EchoHandler())
	defer s.Close()
	r := runner.New(subT, s.URL)
	var logs []string
	r.Log = func(s string) {
		logs = append(logs, s)
	}
	g, err := parse.ParseFile("../testfiles/failure/echo.failure.wrongbody.silk.md")
	is.NoErr(err)
	r.RunGroup(g...)
	is.True(subT.Failed())
	logstr := strings.Join(logs, "\n")
	is.True(strings.Contains(logstr, "body expected:"))
	is.True(strings.Contains(logstr, "GET /echo"))
	is.True(strings.Contains(logstr, "Hello silky."))
	is.True(strings.Contains(logstr, "actual:"))
	is.True(strings.Contains(logstr, "GET /echo"))
	is.True(strings.Contains(logstr, "Hello silk."))
	is.True(strings.Contains(logstr, "--- FAIL: GET /echo"))
	is.True(strings.Contains(logstr, "../testfiles/failure/echo.failure.wrongbody.silk.md:14 - body doesn't match"))
}