-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support to calculate the API coverage
- Loading branch information
1 parent
909341b
commit c651835
Showing
9 changed files
with
309 additions
and
3 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
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,98 @@ | ||
package apispec | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type Swagger struct { | ||
Swagger string `json:"swagger"` | ||
Paths map[string]map[string]SwaggerAPI `json:"paths"` | ||
Info SwaggerInfo `json:"info"` | ||
} | ||
|
||
type SwaggerAPI struct { | ||
OperationId string `json:"operationId"` | ||
Summary string `json:"summary"` | ||
} | ||
|
||
type SwaggerInfo struct { | ||
Description string `json:"description"` | ||
Title string `json:"title"` | ||
Version string `json:"version"` | ||
} | ||
|
||
type APIConverage interface { | ||
HaveAPI(path, method string) (exist bool) | ||
APICount() (count int) | ||
} | ||
|
||
// HaveAPI check if the swagger has the API. | ||
// If the path is /api/v1/names/linuxsuren, then will match /api/v1/names/{name} | ||
func (s *Swagger) HaveAPI(path, method string) (exist bool) { | ||
method = strings.ToLower(method) | ||
for item := range s.Paths { | ||
if matchAPI(path, item) { | ||
for m := range s.Paths[item] { | ||
if strings.ToLower(m) == method { | ||
exist = true | ||
return | ||
} | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
func matchAPI(particularAPI, swaggerAPI string) (matched bool) { | ||
result := swaggerAPIConvert(swaggerAPI) | ||
reg, err := regexp.Compile(result) | ||
if err == nil { | ||
matched = reg.MatchString(particularAPI) | ||
} | ||
return | ||
} | ||
|
||
func swaggerAPIConvert(text string) (result string) { | ||
result = text | ||
reg, err := regexp.Compile("{.*}") | ||
if err == nil { | ||
result = reg.ReplaceAllString(text, ".*") | ||
} | ||
return | ||
} | ||
|
||
// APICount return the count of APIs | ||
func (s *Swagger) APICount() (count int) { | ||
for path := range s.Paths { | ||
for range s.Paths[path] { | ||
count++ | ||
} | ||
} | ||
return | ||
} | ||
|
||
func ParseToSwagger(data []byte) (swagger *Swagger, err error) { | ||
swagger = &Swagger{} | ||
err = json.Unmarshal(data, swagger) | ||
return | ||
} | ||
|
||
func ParseURLToSwagger(swaggerURL string) (swagger *Swagger, err error) { | ||
var resp *http.Response | ||
if resp, err = http.Get(swaggerURL); err == nil && resp != nil && resp.StatusCode == http.StatusOK { | ||
swagger, err = ParseStreamToSwagger(resp.Body) | ||
} | ||
return | ||
} | ||
|
||
func ParseStreamToSwagger(stream io.Reader) (swagger *Swagger, err error) { | ||
var data []byte | ||
if data, err = io.ReadAll(stream); err == nil { | ||
swagger, err = ParseToSwagger(data) | ||
} | ||
return | ||
} |
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,111 @@ | ||
package apispec_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
_ "embed" | ||
|
||
"github.com/h2non/gock" | ||
"github.com/linuxsuren/api-testing/pkg/apispec" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseURLToSwagger(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
swaggerURL string | ||
verify func(t *testing.T, swagger *apispec.Swagger, err error) | ||
}{{ | ||
name: "normal", | ||
swaggerURL: "http://foo", | ||
verify: func(t *testing.T, swagger *apispec.Swagger, err error) { | ||
assert.NoError(t, err) | ||
assert.Equal(t, "2.0", swagger.Swagger) | ||
assert.Equal(t, apispec.SwaggerInfo{ | ||
Description: "sample", | ||
Title: "sample", | ||
Version: "1.0.0", | ||
}, swagger.Info) | ||
}, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gock.New(tt.swaggerURL).Get("/").Reply(200).BodyString(testdataSwaggerJSON) | ||
defer gock.Off() | ||
|
||
s, err := apispec.ParseURLToSwagger(tt.swaggerURL) | ||
tt.verify(t, s, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestHaveAPI(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
swaggerURL string | ||
path, method string | ||
expectExist bool | ||
}{{ | ||
name: "normal, exist", | ||
swaggerURL: "http://foo", | ||
path: "/api/v1/users", | ||
method: http.MethodGet, | ||
expectExist: true, | ||
}, { | ||
name: "create user, exist", | ||
swaggerURL: "http://foo", | ||
path: "/api/v1/users", | ||
method: http.MethodPost, | ||
expectExist: true, | ||
}, { | ||
name: "get a user, exist", | ||
swaggerURL: "http://foo", | ||
path: "/api/v1/users/linuxsuren", | ||
method: http.MethodGet, | ||
expectExist: true, | ||
}, { | ||
name: "normal, not exist", | ||
swaggerURL: "http://foo", | ||
path: "/api/v1/users", | ||
method: http.MethodDelete, | ||
expectExist: false, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gock.New(tt.swaggerURL).Get("/").Reply(200).BodyString(testdataSwaggerJSON) | ||
defer gock.Off() | ||
|
||
swagger, err := apispec.ParseURLToSwagger(tt.swaggerURL) | ||
assert.NoError(t, err) | ||
exist := swagger.HaveAPI(tt.path, tt.method) | ||
assert.Equal(t, tt.expectExist, exist) | ||
}) | ||
} | ||
} | ||
|
||
func TestAPICount(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
swaggerURL string | ||
expectCount int | ||
}{{ | ||
name: "normal", | ||
swaggerURL: "http://foo", | ||
expectCount: 5, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gock.New(tt.swaggerURL).Get("/").Reply(200).BodyString(testdataSwaggerJSON) | ||
defer gock.Off() | ||
|
||
swagger, err := apispec.ParseURLToSwagger(tt.swaggerURL) | ||
assert.NoError(t, err) | ||
count := swagger.APICount() | ||
assert.Equal(t, tt.expectCount, count) | ||
}) | ||
} | ||
} | ||
|
||
//go:embed testdata/swagger.json | ||
var testdataSwaggerJSON string |
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,34 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "sample", | ||
"title": "sample", | ||
"version": "1.0.0" | ||
}, | ||
"paths": { | ||
"/api/v1/users": { | ||
"get": { | ||
"summary": "summary", | ||
"operationId": "getUsers" | ||
}, | ||
"post": { | ||
"summary": "summary", | ||
"operationId": "createUser" | ||
} | ||
}, | ||
"/api/v1/users/{user}": { | ||
"get": { | ||
"summary": "summary", | ||
"operationId": "getUser" | ||
}, | ||
"delete": { | ||
"summary": "summary", | ||
"operationId": "deleteUser" | ||
}, | ||
"put": { | ||
"summary": "summary", | ||
"operationId": "updateUser" | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.