Skip to content

Commit

Permalink
custom: custom method support with template added
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanphayade committed Jul 9, 2020
1 parent 703ef2e commit 9589010
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![inpostor](impostor.png)
![impostor](impostor.png)

# Impostor
Impostor is a REST API simulator (Mock server) written in golang. It'll runs based on a configuration provided to simulate various use cases.
Expand Down
5 changes: 5 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
port = 9000
host = "localhost"
mock_path = "test"

[apps.settlements]
port = 9001
host = "localhost"
mock_path = "test"
4 changes: 2 additions & 2 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type GeneratorFunc func(n int) string

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

func GenerateString(n int) string {
func generateString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}

func GenerateInt(n int) string {
func generateInt(n int) string {
return fmt.Sprintf("%d", rand.Int63n(1e16))[0:n]
}
28 changes: 26 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ import (
const (
ResolverGenerator = "generate"
ResolverCopy = "copy"
ResolverCustom = "custom"

GeneratorString = "string"
GeneratorInt = "int"

CustomConstant = "constant"
)

var (
generator = map[string]GeneratorFunc{
GeneratorString: GenerateString,
GeneratorInt: GenerateInt,
GeneratorString: generateString,
GeneratorInt: generateInt,
}

call = map[string]callerFunc{
CustomConstant: callConcat,
}
)

Expand Down Expand Up @@ -67,6 +74,9 @@ func resolve(key string, tokens []string, data collector) string {

case ResolverCopy:
return copyFrom(tokens, data)

case ResolverCustom:
return customCall(data, tokens)
}

return ""
Expand Down Expand Up @@ -96,5 +106,19 @@ func generate(tokens []string) string {
return gen(val)
}

log.Println("error: specified generator not found")
return ""
}

func customCall(data collector, tokens []string) string {
if len(tokens) < 1 {
log.Println("error: invalid number of arguments in call")
return ""
}

if caller, ok := call[tokens[0]]; ok {
return caller(data, tokens[1:]...)
}

return ""
}
2 changes: 1 addition & 1 deletion test/app/create_users.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"response": {
"label": "create",
"body": "{\"id\":\"{{ generate string 14 }}\",\"firstname\":\"{{ copy body firstname }}\",\"lastname\":\"{{ copy body lastname }}\",\"last_seen\":\"{{ copy body option.status }}\"}",
"body": "{\"id\":\"{{ generate string 14 }}\",\"firstname\":\"{{ copy body firstname }}\",\"lastname\":\"{{ custom constant }}\",\"last_seen\":\"{{ copy body option.status }}\"}",
"latency": 0,
"status_code": 201,
"headers": {
Expand Down

0 comments on commit 9589010

Please sign in to comment.