-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test case & docs for provider versions endpoint
- Loading branch information
Shu Kutsuzawa
committed
Nov 11, 2020
1 parent
da8c622
commit 1fc26e7
Showing
4 changed files
with
146 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# API doc | ||
|
||
This is API documentation for Provider versions. This is generated by `httpdoc`. Don't edit by hand. | ||
|
||
## Table of contents | ||
|
||
- [[200] GET /v1/providers/cappyzawa/concourse/versions](#200-get-v1providerscappyzawaconcourseversions) | ||
- [[404] GET /v1/providers/foo/bar/versions](#404-get-v1providersfoobarversions) | ||
|
||
|
||
## [200] GET /v1/providers/cappyzawa/concourse/versions | ||
|
||
existing provider: cappyzawa/concourse | ||
|
||
### Request | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
### Response | ||
|
||
|
||
|
||
|
||
|
||
Response example | ||
|
||
<details> | ||
<summary>Click to expand code.</summary> | ||
|
||
```javascript | ||
{"versions":[{"version":"0.1.0","protocols":["5.3"],"platforms":[{"os":"darwin","arch":"amd64"},{"os":"linux","arch":"amd64"}]},{"version":"0.0.5","protocols":["5.3"],"platforms":[{"os":"darwin","arch":"amd64"},{"os":"linux","arch":"amd64"}]}]} | ||
|
||
``` | ||
|
||
</details> | ||
|
||
|
||
## [404] GET /v1/providers/foo/bar/versions | ||
|
||
non existing provider: foo/bar | ||
|
||
### Request | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
### Response | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
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,9 +1,79 @@ | ||
package provider_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/cappyzawa/terraform-registry/config" | ||
"github.com/cappyzawa/terraform-registry/handler/provider" | ||
"github.com/go-chi/chi" | ||
"go.mercari.io/go-httpdoc" | ||
) | ||
|
||
func TestVersionHandlerServeHTTP(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
name string | ||
namespace string | ||
_type string | ||
expectStatus int | ||
}{ | ||
{ | ||
name: "existing provider: cappyzawa/concourse", | ||
namespace: "cappyzawa", | ||
_type: "concourse", | ||
expectStatus: http.StatusOK, | ||
}, | ||
{ | ||
name: "non existing provider: foo/bar", | ||
namespace: "foo", | ||
_type: "bar", | ||
expectStatus: http.StatusNotFound, | ||
}, | ||
} | ||
|
||
document := &httpdoc.Document{ | ||
Name: "Provider versions", | ||
} | ||
defer func() { | ||
if err := document.Generate("../../docs/provider/versions.md"); err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
}() | ||
|
||
for _, test := range cases { | ||
t.Run(test.name, func(t *testing.T) { | ||
ts := testServer(document, test.name) | ||
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/providers/%s/%s/versions", ts.URL, test.namespace, test._type), nil) | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
defer res.Body.Close() | ||
if res.StatusCode != test.expectStatus { | ||
t.Errorf("status code should be %v, but it is %v", test.expectStatus, res.StatusCode) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testServer(doc *httpdoc.Document, description string) *httptest.Server { | ||
cfg, _ := config.Parse("../../testdata/config.yaml") | ||
pvh := &provider.VersionsHandler{ | ||
Providers: cfg.Providers, | ||
} | ||
|
||
r := chi.NewRouter() | ||
r.Use(func(next http.Handler) http.Handler { | ||
return httpdoc.Record(next, doc, &httpdoc.RecordOption{ | ||
Description: description, | ||
ExcludeHeaders: []string{"Content-Length", "User-Agent", "Accept-Encoding"}, | ||
}) | ||
}) | ||
r.Get("/v1/providers/{namespace}/{type}/versions", pvh.ServeHTTP) | ||
|
||
return httptest.NewServer(r) | ||
} |
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