Skip to content

Commit

Permalink
add test case & docs for provider versions endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Shu Kutsuzawa committed Nov 11, 2020
1 parent da8c622 commit 1fc26e7
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
65 changes: 65 additions & 0 deletions docs/provider/versions.md
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







2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
70 changes: 70 additions & 0 deletions handler/provider/versions_test.go
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)
}
10 changes: 10 additions & 0 deletions testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: []

0 comments on commit 1fc26e7

Please sign in to comment.