I'm getting a request validation failed
when running a test in golang but not when running the code:
{"error":"Request validation failed","validation":[{"keyword":"type","dataPath":".body","schemaPath":"#/properties/body/type","params":{"type":"object"},"message":"should be object"}]}
If I run the code normally, everything is working fine (no errors). However, if I run the test which is calling the same functions is giving that error. The json file that I'm processing is exactly the same.
How is that possible? Is there any part of the code that I should double check to make the test working correctly?
This is the test func:
func TestSync(t *testing.T) {
var url_base string = "http://localhost:8080"
var token string = "xxx"
fileJson := EnvVars("test3_0.json")
Sync(url_base, token, fileJson)
responseServer := StatusFeaturesServer(url_base+"/api/admin/projects/default", token)
expected, _ := json.Marshal(fileJson)
server, _ := json.Marshal(responseServer)
if !cmp.Equal(responseServer, fileJson) {
t.Errorf("Expected '%s', but got '%s'", string(expected), string(server))
}
}
This is the code:
// JSON structure file
type Features struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
}
type Environment struct {
Environment string `json:"environment"`
Features []Features `json:"features"`
}
type Environments struct {
Environments []Environment `json:"environments"`
}
func EnvVars(file_json string) *Environments {
// Open our json file
jsonFile, err := os.Open(file_json)
if err != nil {
fmt.Println(err)
}
// defer the closing of the jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened jsonFile as a byte array.
byteValue, errioutil := io.ReadAll(jsonFile)
if errioutil != nil {
fmt.Println(errioutil)
}
// we initialize our features array
var dataJson Environments
// we unmarshal our byteArray which contains our
// jsonFile's content into 'dataJson' which we defined above
errjson := json.Unmarshal([]byte(byteValue), &dataJson)
if errjson != nil {
fmt.Println(errjson)
}
fmt.Println("Successfully processed json file")
return &dataJson
}
func Sync(url_base string, token string, fileJson *Environments) {
for _, e := range fileJson.Environments {
for _, f := range e.Features {
FFSync(url_base, token, f.Name, e.Environment, strconv.FormatBool(f.Enabled))
}
}
}
func FFSync(url_base string, token string, feature string, environment string, enabled string) {
url := fmt.Sprintf(url_base+"/api/admin/projects/default/features/%s/environments/%s/%s", feature, environment, enabled)
ApiCall(token, nil, url)
}
func ApiCall(token string, data interface{}, url string) {
payloadBytes, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
body := bytes.NewReader(payloadBytes)
client := &http.Client{}
req, err := http.NewRequest("POST", url, body)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", token)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", string(bodyText))
}