jokes-bapak2/api/app/v1/utils/parse_test.go

45 lines
1.1 KiB
Go
Raw Normal View History

2021-07-09 12:13:19 +00:00
package utils_test
import (
"strings"
2021-07-09 12:13:19 +00:00
"testing"
2021-07-14 18:17:01 +00:00
"jokes-bapak2-api/app/v1/utils"
2021-07-09 12:13:19 +00:00
)
func TestParseToJSONBody(t *testing.T) {
t.Run("should be able to parse a json string", func(t *testing.T) {
body := map[string]interface{}{
"name": "Scott",
"age": 32,
"fat": true,
}
parsed, err := utils.ParseToJSONBody(body)
if err != nil {
t.Error(err.Error())
}
result := "{\"age\":32,\"fat\":true,\"name\":\"Scott\"}"
if string(parsed) != result {
t.Error("parsed string is not the same as result:", string(parsed))
}
})
}
func TestParseToFormBody(t *testing.T) {
t.Run("should be able to parse a form body", func(t *testing.T) {
body := map[string]interface{}{
"age": 32,
"fat": true,
"name": "Scott",
2021-07-09 12:13:19 +00:00
}
parsed, err := utils.ParseToFormBody(body)
if err != nil {
t.Error(err.Error())
}
result := [3]string{"age=32&", "fat=true&", "name=Scott&"}
if !strings.Contains(string(parsed), result[0]) && !strings.Contains(string(parsed), result[1]) && !strings.Contains(string(parsed), result[2]) {
2021-07-09 12:13:19 +00:00
t.Error("parsed string is not the same as result:", string(parsed))
}
})
}