|
package tests
import (
"encoding/json"
"errors"
"unicode"
"unicode/utf8"
"github.com/verdverm/frisby"
)
const groupsURL = apiURL + "groups"
|
Group represents part of response containing list of groups
|
type Group struct {
Description string `json:"description"`
Title string `json:"title"`
Tags [ ] string `json:"tags"`
}
|
GroupsResponse represents response containing list of groups
|
type GroupsResponse struct {
Groups [ ] Group `json:"groups"`
Status string `json:"status"`
}
func checkGroupsResponseContent ( payload [ ] byte ) error {
response := GroupsResponse { }
|
check if the 'groups' response has proper format
|
err := json . Unmarshal ( payload , & response )
if err != nil {
|
deserialization failed
|
return err
}
if response . Status != "ok" {
|
unexpected status detected
|
return errors . New ( "ok status expected" )
}
for _ , group := range response . Groups {
err := checkTextAttribute ( group . Title , "title" )
if err != nil {
return err
}
err = checkTextAttribute ( group . Description , "description" )
if err != nil {
return err
}
}
|
everything seems to be ok
|
return nil
}
|
checkTextAttribute is a rudimentary check for text attributes
|
func checkTextAttribute ( text , what string ) error {
|
check for empty string
|
if text == "" {
return errors . New ( "empty " + what + " detected in a group" )
}
|
check the capitalization
|
firstRune , _ := utf8 . DecodeRuneInString ( text )
if ! unicode . IsUpper ( firstRune ) {
return errors . New ( what + " should start by uppercase letter" )
}
|
everything seems to be ok
|
return nil
}
|
checkGroupsEndpoint checks whether 'groups' endpoint is handled correctly
|
func checkGroupsEndpoint ( ) {
f := frisby . Create ( "Check the 'groups' endpoint" ) . Get ( groupsURL )
f . Send ( )
f . ExpectStatus ( 200 )
f . ExpectHeader ( contentTypeHeader , "application/json; charset=utf-8" )
f . PrintReport ( )
|
try to read payload
|
text , err := f . Resp . Content ( )
if err != nil {
f . AddError ( err . Error ( ) )
return
}
|
payload seems to part of response - let's check its content
|
err = checkGroupsResponseContent ( text )
if err != nil {
f . AddError ( err . Error ( ) )
}
}
|
checkWrongMethodsForGroupsEndpoint check whether other HTTP methods are rejected correctly for the REST API 'groups' point
|
func checkWrongMethodsForGroupsEndpoint ( ) {
checkGetEndpointByOtherMethods ( groupsURL , false )
}
|