Skip to content

Commit

Permalink
feature/read-yaml-and-load-routes
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonysyk committed Jan 5, 2023
1 parent 7210be7 commit 2717f0f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 96 deletions.
105 changes: 105 additions & 0 deletions conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package http_server_mock

import (
"errors"
"gopkg.in/yaml.v3"
"os"
)

func GetRoutes(config string) (Routes, error) {
m := make(map[interface{}]interface{})
err := yaml.Unmarshal([]byte(config), &m)
if err != nil {
return nil, err
}
paths := getPaths(m)
var routes Routes
for _, ut := range paths {
methodTails := getMethod(ut.Tail)
for _, mt := range methodTails {
statusTails := getStatus(mt.Tail)
for _, st := range statusTails {
filepath, body, bodyErr := getBody(st.Tail)
routes = append(routes, Route{
URL: ut.URL,
Method: mt.Method,
StatusCode: st.Status,
Body: body,
Filepath: filepath,
Errors: []error{bodyErr},
})
}
}
}
return routes, nil
}

type URLTail struct {
URL string
Tail map[string]interface{}
}

func getPaths(m map[interface{}]interface{}) []URLTail {
var URLTails []URLTail
for url, path := range m["paths"].(map[string]interface{}) {
URLTails = append(URLTails, URLTail{URL: url, Tail: path.(map[string]interface{})})
}
return URLTails
}

type MethodTail struct {
Method string
Tail map[interface{}]interface{}
}

func getMethod(m map[string]interface{}) []MethodTail {
var methodTails []MethodTail
for k, v := range m {
methodTail := MethodTail{
Method: k,
Tail: v.(map[interface{}]interface{}),
}
methodTails = append(methodTails, methodTail)
}

return methodTails
}

type StatusTail struct {
Status int
Tail map[string]interface{}
}

func getStatus(m map[interface{}]interface{}) []StatusTail {
var statusTails []StatusTail
for k, v := range m {
if _, ok := k.(int); !ok {
panic("status code must be an integer")
}
statusTails = append(statusTails, StatusTail{
Status: k.(int),
Tail: v.(map[string]interface{}),
})
}

return statusTails
}

func getBody(m map[string]interface{}) (string, []byte, error) {
if len(m) > 1 {
return "", nil, errors.New("cannot set both body and filepath for a specific status code")
}
if body, ok := m["body"]; ok && body != "" {
return "", []byte(body.(string)), nil
}
if filepath, ok := m["filepath"]; ok && filepath != "" {
fp := filepath.(string)
content, err := os.ReadFile(fp)
if err != nil {
return fp, nil, err
}
return fp, content, nil
}

return "", nil, errors.New("no body found")
}
103 changes: 7 additions & 96 deletions example/conf/conf_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package conf

import (
"errors"
"fmt"
"github.com/anthonysyk/http-server-mock"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"

_ "embed"
Expand All @@ -14,99 +12,12 @@ import (
var RoutesYAML string

func TestModel(t *testing.T) {
m := make(map[interface{}]interface{})
err := yaml.Unmarshal([]byte(RoutesYAML), &m)
routes, err := http_server_mock.GetRoutes(RoutesYAML)
assert.NoError(t, err)
paths := getPaths(m)
for _, ut := range paths {
//fmt.Printf("%+v\n", ut)
methodTails := getMethod(ut.URL, ut.Tail)
for _, mt := range methodTails {
//fmt.Printf("%+v\n", mt)
statusTails := getStatus(mt.Tail)
for _, st := range statusTails {
fmt.Printf("%s, %s, %v, tail : %+v\n", ut.URL, mt.Method, st.Status, st.Tail)
}
}
}
assert.Len(t, routes, 8)
}

type URLTail struct {
URL string
Tail map[string]interface{}
}

func getPaths(m map[interface{}]interface{}) []URLTail {
var URLTails []URLTail
for url, path := range m["paths"].(map[string]interface{}) {
URLTails = append(URLTails, URLTail{URL: url, Tail: path.(map[string]interface{})})
}
return URLTails
}

type MethodTail struct {
Method string
Tail map[interface{}]interface{}
}

func getMethod(url string, m map[string]interface{}) []MethodTail {
var methodTails []MethodTail
for k, v := range m {
methodTail := MethodTail{
Method: k,
Tail: v.(map[interface{}]interface{}),
}
methodTails = append(methodTails, methodTail)
}

return methodTails
}

type StatusTail struct {
Status int
Tail map[string]interface{}
}

func getStatus(m map[interface{}]interface{}) []StatusTail {
var statusTails []StatusTail
for k, v := range m {
if _, ok := k.(int); !ok {
panic("status code must be an integer")
}
statusTails = append(statusTails, StatusTail{
Status: k.(int),
Tail: v.(map[string]interface{}),
})
}

return statusTails
}

func getBody(url, method string, statusCode int, m map[string]interface{}) ([]byte, error) {
if len(m) > 1 {
return nil, errors.New("cannot set multiple bodies for a specific status code")
}
for _, t := range m {
return t.([]byte), nil
}
return nil, errors.New("no body found")
}

func getFilepath(m map[string]interface{}) (string, error) {
if len(m) > 1 {
return "nil", errors.New("cannot set multiple filenames for a specific status code")
}
for _, t := range m {
return t.(string), nil
}
return "", errors.New("no filename found")
}

type Routes []Route
type Route struct {
URL string
Method string
StatusCode int
Body []byte
Filepath string
}
// run server
// do some calls
// catch output stdout
// verify output is ok
12 changes: 12 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package http_server_mock

type Routes []Route

type Route struct {
URL string
Method string
StatusCode int
Body []byte
Filepath string
Errors []error
}
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package http_server_mock

0 comments on commit 2717f0f

Please sign in to comment.