This repository has been archived by the owner on May 11, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #52: prepare integration tests
- Loading branch information
Showing
19 changed files
with
277 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<form id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4" title="Email subscription" action="http://localhost:8080/api/v1/41ca5e09-3ce2-4094-b108-3ecc257c6fa4" method="post" enctype="application/x-www-form-urlencoded"><label for="41ca5e09-3ce2-4094-b108-3ecc257c6fa4_email">Email</label><input id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4_email" name="email" type="email" title="Email" maxlength="64" required><input id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4__redirect" name="_redirect" type="hidden" value="https://kamil.samigullin.info/"><input type="submit"></form> | ||
<form id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4" title="Email subscription" action="http://form-api.dev/api/v1/41ca5e09-3ce2-4094-b108-3ecc257c6fa4" method="post" enctype="application/x-www-form-urlencoded"><label for="41ca5e09-3ce2-4094-b108-3ecc257c6fa4_email">Email</label><input id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4_email" name="email" type="email" title="Email" maxlength="64" required><input id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4__redirect" name="_redirect" type="hidden" value="https://kamil.samigullin.info/"><input type="submit"></form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<form id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4" title="Email subscription" action="http://localhost:8080/api/v1/41ca5e09-3ce2-4094-b108-3ecc257c6fa4" method="post" enctype="application/x-www-form-urlencoded"> | ||
<form id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4" title="Email subscription" action="http://form-api.dev/api/v1/41ca5e09-3ce2-4094-b108-3ecc257c6fa4" method="post" enctype="application/x-www-form-urlencoded"> | ||
<input id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4_email" name="email" type="email" title="Email" maxlength="64" required="true"/> | ||
<input id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4__redirect" name="_redirect" type="hidden" value="https://kamil.samigullin.info/"/> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<form id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4" title="Email subscription" action="http://localhost:8080/api/v1/41ca5e09-3ce2-4094-b108-3ecc257c6fa4" method="post" enctype="application/x-www-form-urlencoded"> | ||
<form id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4" title="Email subscription" action="http://form-api.dev/api/v1/41ca5e09-3ce2-4094-b108-3ecc257c6fa4" method="post" enctype="application/x-www-form-urlencoded"> | ||
<input id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4_email" name="email" type="email" title="Email" maxlength="64" required="true"></input> | ||
<input id="41ca5e09-3ce2-4094-b108-3ecc257c6fa4__redirect" name="_redirect" type="hidden" value="https://kamil.samigullin.info/"></input> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<form title="Email subscription" action="http://localhost:8080/api/v1/41ca5e09-3ce2-4094-b108-3ecc257c6fa4"> | ||
<form title="Email subscription" action="http://form-api.dev/api/v1/41ca5e09-3ce2-4094-b108-3ecc257c6fa4"> | ||
<input name="email" type="email" title="Email" maxlength="64" required="true"></input> | ||
<input name="_redirect" type="hidden" value="https://kamil.samigullin.info/"></input> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
//go:generate echo $PWD/$GOPACKAGE/$GOFILE | ||
//go:generate mockgen -package main -destination $PWD/mock_storage_test.go github.com/kamilsk/form-api/service Storage | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"path" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/kamilsk/form-api/domen" | ||
"github.com/kamilsk/form-api/errors" | ||
"github.com/kamilsk/form-api/server" | ||
"github.com/kamilsk/form-api/server/router/chi" | ||
"github.com/kamilsk/form-api/service" | ||
"github.com/kamilsk/form-api/transfer/encoding" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
HOST = "http://form-api.dev/" | ||
APIv1 = "api/v1" | ||
UUID = domen.UUID("41ca5e09-3ce2-4094-b108-3ecc257c6fa4") | ||
ZERO = domen.UUID("00000000-0000-0000-0000-000000000000") | ||
) | ||
|
||
func TestAPI_GetV1(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
var ( | ||
storage = NewMockStorage(ctrl) | ||
) | ||
|
||
handler := chi.NewRouter(server.New(HOST, "", service.New(storage)), false) | ||
srv := httptest.NewServer(handler) | ||
defer srv.Close() | ||
|
||
{ | ||
var ( | ||
expected = domen.Schema{ | ||
ID: UUID.String(), | ||
Title: "Email subscription", | ||
Action: join(HOST, APIv1, UUID.String()), | ||
Method: "post", | ||
EncodingType: "application/x-www-form-urlencoded", | ||
Inputs: []domen.Input{ | ||
{ | ||
ID: UUID.String() + "_email", | ||
Name: "email", | ||
Type: "email", | ||
Title: "Email", | ||
MaxLength: 64, | ||
Required: true, | ||
}, | ||
{ | ||
ID: UUID.String() + "__redirect", | ||
Name: "_redirect", | ||
Type: "hidden", | ||
Value: "https://kamil.samigullin.info/", | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
request *http.Request | ||
golden string | ||
}{ | ||
{"get schema as HTML", func() *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
req.Header.Set("Accept", encoding.HTML) | ||
return req | ||
}(), "./transfer/encoding/fixtures/email_subscription.html.golden"}, | ||
{"get schema as JSON", func() *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
req.Header.Set("Accept", encoding.JSON) | ||
return req | ||
}(), "./transfer/encoding/fixtures/email_subscription.json.golden"}, | ||
{"get schema as XML", func() *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
req.Header.Set("Accept", encoding.XML) | ||
return req | ||
}(), "./transfer/encoding/fixtures/email_subscription.xml.golden"}, | ||
{"get schema as text", func() *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
req.Header.Set("Accept", encoding.TEXT) | ||
return req | ||
}(), "./transfer/encoding/fixtures/email_subscription.yaml.golden"}, | ||
} | ||
storage.EXPECT().Schema(UUID).Times(len(tests)).Return(expected, nil) | ||
|
||
for _, test := range tests { | ||
tc := test | ||
t.Run(test.name, func(t *testing.T) { | ||
resp, err := http.DefaultClient.Do(tc.request) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
expected, err := ioutil.ReadFile(tc.golden) | ||
assert.NoError(t, err) | ||
obtained, err := ioutil.ReadAll(resp.Body) | ||
assert.NoError(t, err) | ||
assert.NoError(t, resp.Body.Close()) | ||
assert.Equal(t, expected, obtained) | ||
}) | ||
} | ||
} | ||
|
||
{ | ||
tests := []struct { | ||
name string | ||
request *http.Request | ||
code int | ||
}{ | ||
{http.StatusText(http.StatusBadRequest), func() *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, ZERO.String()), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return req | ||
}(), http.StatusBadRequest}, | ||
{http.StatusText(http.StatusNotAcceptable), func() *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
req.Header.Set("Accept", "application/toml") | ||
return req | ||
}(), http.StatusNotAcceptable}, | ||
{http.StatusText(http.StatusNotFound), func() *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return req | ||
}(), http.StatusNotFound}, | ||
} | ||
storage.EXPECT().Schema(UUID).Times(1).Return(domen.Schema{}, errors.NotFound("", nil, "")) | ||
|
||
for _, test := range tests { | ||
tc := test | ||
t.Run(test.name, func(t *testing.T) { | ||
resp, err := http.DefaultClient.Do(tc.request) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.code, resp.StatusCode) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func TestAPI_PostV1(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
} | ||
|
||
func join(base string, paths ...string) string { | ||
u, err := url.Parse(base) | ||
if err != nil { | ||
panic(err) | ||
} | ||
u.Path = path.Join(append([]string{u.Path}, paths...)...) | ||
return u.String() | ||
} |
Oops, something went wrong.