0

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))

}
X T
  • 445
  • 6
  • 22
  • 2
    Can you add the (relevant parts of the) `EnvVars` function to the posted code? – Norbert Sep 27 '22 at 16:31
  • 1
    @NorbertvanNobelen code updated in the main post. – X T Sep 28 '22 at 08:35
  • 1
    Not having ran the code however and not having a line for the error however: ` if cmp.Equal(responseServer, fileJson) {` Is comparing a pointer with something And your logline afterwards states: `t.Errorf("Expected '%s', but got '%s'", string(expected), string(server))` Which are 2 completely different strings/objects/not the pointers. – Norbert Sep 28 '22 at 21:14
  • @NorbertvanNobelen I forgot to mention that "expected" and "server" are two identical structures. I'm converting the jsonfile into an structure and also the response of the server into another structure (but i have another function that adapts this server's response into an structure compatible to the previous one). This is working as I have other tests (to create/delete features) and it's working fine. The code contains more functions but basically, the test is failing in the TestSync throwing this error "{"error":"Request validation failed..." – X T Sep 29 '22 at 07:25
  • Any idea on what's going on in this func test? – X T Oct 04 '22 at 07:37
  • I've already resolved the issue. I was using a newer version of the image that was running into the container so the golang code was not working as expected for that version. After using an older version of the container image that I was using everything worked fine. – X T Oct 10 '22 at 10:56

0 Answers0