Skip to content

Commit

Permalink
add unit tests for backend configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dikhan committed Oct 11, 2018
1 parent 535b6b0 commit df3a90e
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 1 deletion.
2 changes: 1 addition & 1 deletion openapi/openapi_v2_backend_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type specV2BackendConfiguration struct {

func newOpenAPIBackendConfigurationV2(spec *spec.Swagger, openAPIDocumentURL string) (*specV2BackendConfiguration, error) {
if spec.Swagger != "2.0" {
return nil, fmt.Errorf("swagger version '%s' not supported, specV2BackendConfiguration only supports 2.0 ", spec.Swagger)
return nil, fmt.Errorf("swagger version '%s' not supported, specV2BackendConfiguration only supports 2.0", spec.Swagger)
}
if openAPIDocumentURL == "" {
return nil, fmt.Errorf("missing mandatory parameter openAPIDocumentURL")
Expand Down
170 changes: 170 additions & 0 deletions openapi/openapi_v2_backend_configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package openapi

import (
"github.com/go-openapi/spec"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestNewOpenAPIBackendConfigurationV2(t *testing.T) {
Convey("Given a swagger spec 2.0 and an openAPIDocumentURL", t, func() {
spec := &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger:"2.0",
},
}
openAPIDocumentURL := "www.domain.com"
Convey("When newOpenAPIBackendConfigurationV2 method is called", func() {
specV2BackendConfiguration, err := newOpenAPIBackendConfigurationV2(spec, openAPIDocumentURL)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Then the providerClient should comply with SpecBackendConfiguration interface", func() {
var _ SpecBackendConfiguration = specV2BackendConfiguration
})
})
})

Convey("Given a swagger spec that is not supported 3.0 and an openAPIDocumentURL", t, func() {
spec := &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger:"3.0",
},
}
openAPIDocumentURL := "www.domain.com"
Convey("When newOpenAPIBackendConfigurationV2 method is called", func() {
_, err := newOpenAPIBackendConfigurationV2(spec, openAPIDocumentURL)
Convey("Then the error returned should be NOT nil", func() {
So(err, ShouldNotBeNil)
})
Convey("And the error message should be", func() {
So(err.Error(), ShouldEqual, "swagger version '3.0' not supported, specV2BackendConfiguration only supports 2.0")
})
})
})

Convey("Given a swagger spec 2.0 and an empty openAPIDocumentURL", t, func() {
spec := &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger:"2.0",
},
}
openAPIDocumentURL := ""
Convey("When newOpenAPIBackendConfigurationV2 method is called", func() {
_, err := newOpenAPIBackendConfigurationV2(spec, openAPIDocumentURL)
Convey("Then the error returned should be NOT nil", func() {
So(err, ShouldNotBeNil)
})
Convey("And the error message should be", func() {
So(err.Error(), ShouldEqual, "missing mandatory parameter openAPIDocumentURL")
})
})
})
}


func TestGetHost(t *testing.T) {
Convey("Given a specV2BackendConfiguration with the host configured", t, func() {
spec := &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0",
Host: "www.some-backend.com",
},
}
openAPIDocumentURL := "www.domain.com"
specV2BackendConfiguration, _ := newOpenAPIBackendConfigurationV2(spec, openAPIDocumentURL)
Convey("When getHost method is called", func() {
host, err := specV2BackendConfiguration.getHost()
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the host should be correct", func() {
So(host, ShouldEqual, "www.some-backend.com")
})
})
})

Convey("Given a specV2BackendConfiguration with the host not configured", t, func() {
spec := &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0",
Host: "",
},
}
openAPIDocumentURL := "www.domain.com"
specV2BackendConfiguration, _ := newOpenAPIBackendConfigurationV2(spec, openAPIDocumentURL)
Convey("When getHost method is called", func() {
host, err := specV2BackendConfiguration.getHost()
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the host should be the one where the swagger file is being served", func() {
So(host, ShouldEqual, openAPIDocumentURL)
})
})
})

Convey("Given a specV2BackendConfiguration with the host not configured", t, func() {
spec := &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0",
Host: "",
},
}
specV2BackendConfiguration := specV2BackendConfiguration{spec:spec, openAPIDocumentURL:""}
Convey("When getHost method is called", func() {
_, err := specV2BackendConfiguration.getHost()
Convey("Then the error returned should NOT be nil", func() {
So(err, ShouldNotBeNil)
})
Convey("And the error message should be", func() {
So(err.Error(), ShouldEqual, "could not find valid host from URL provided: ''")
})
})
})
}


func TestGetBasePath(t *testing.T) {
Convey("Given a specV2BackendConfiguration with the basePath configured", t, func() {
spec := &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0",
Host: "www.some-backend.com",
BasePath: "/api",
},
}
openAPIDocumentURL := "www.domain.com"
specV2BackendConfiguration, _ := newOpenAPIBackendConfigurationV2(spec, openAPIDocumentURL)
Convey("When getBasePath method is called", func() {
basePath := specV2BackendConfiguration.getBasePath()
Convey("And the host should be correct", func() {
So(basePath, ShouldEqual, "/api")
})
})
})
}


func TestGetHTTPSchemes(t *testing.T) {
Convey("Given a specV2BackendConfiguration with the getHTTPSchemes configured", t, func() {
spec := &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0",
Host: "www.some-backend.com",
BasePath: "/api",
Schemes: []string{"http", "https"},
},
}
openAPIDocumentURL := "www.domain.com"
specV2BackendConfiguration, _ := newOpenAPIBackendConfigurationV2(spec, openAPIDocumentURL)
Convey("When getHTTPSchemes method is called", func() {
httpSchemes := specV2BackendConfiguration.getHTTPSchemes()
Convey("And the host should be correct", func() {
So(httpSchemes, ShouldContain, "http")
So(httpSchemes, ShouldContain, "https")
})
})
})
}

0 comments on commit df3a90e

Please sign in to comment.