diff --git a/docs/provider/versions.md b/docs/provider/versions.md new file mode 100644 index 0000000..a8b9f94 --- /dev/null +++ b/docs/provider/versions.md @@ -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 + +
+Click to expand code. + +```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"}]}]} + +``` + +
+ + +## [404] GET /v1/providers/foo/bar/versions + +non existing provider: foo/bar + +### Request + + + + + + + + + +### Response + + + + + + + diff --git a/go.mod b/go.mod index 62a6284..ae54d03 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,6 @@ require ( github.com/go-chi/chi v4.1.2+incompatible github.com/golang/protobuf v1.4.3 // indirect github.com/tenntenn/gpath v0.0.0-20170604083136-3e6e957a3952 // indirect - go.mercari.io/go-httpdoc v0.2.0 // indirect + go.mercari.io/go-httpdoc v0.2.0 gopkg.in/yaml.v2 v2.3.0 ) diff --git a/handler/provider/versions_test.go b/handler/provider/versions_test.go index 6fdbe74..0671a1f 100644 --- a/handler/provider/versions_test.go +++ b/handler/provider/versions_test.go @@ -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) } diff --git a/testdata/config.yaml b/testdata/config.yaml index d83c00b..76e624e 100644 --- a/testdata/config.yaml +++ b/testdata/config.yaml @@ -6,7 +6,17 @@ providers: platforms: - os: darwin arch: amd64 + - os: linux + arch: amd64 source: download_url: https://github.com/cappyzawa/terraform-provider-concourse/releases/download/v0.1.0/terraform-provider-concourse_0.1.0_darwin_amd64.zip + - name: "0.0.5" + platforms: + - os: darwin + arch: amd64 + - os: linux + arch: amd64 + source: + download_url: https://github.com/cappyzawa/terraform-provider-concourse/releases/download/v0.1.0/terraform-provider-concourse_0.0.5_darwin_amd64.zip modules: []