Skip to content

updateTest: multiple groups collapse to the last one (form encoding omits [])  #24

@jochen-testingbot

Description

@jochen-testingbot

Summary

When calling updateTest with more than one group, only the last group is saved on the test. No error is thrown, so it fails silently.

Cause

In lib/api.js, form-encoded request bodies are built with querystring.stringify(flattenObject(...)). flattenObject only recurses into plain objects, not arrays:

if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
  Object.assign(flattened, flattenObject(value, newKey));
} else {
  flattened[newKey] = value;   // arrays land here
}

So { groups: ['a', 'b'] } becomes querystring.stringify({ groups: ['a', 'b'] })groups=a&groups=b (repeated key, no []).

The TestingBot API parses params with Rack, which collapses repeated bracket-less keys to the last value:

groups[]=a&groups[]=b  =>  {"groups"=>["a", "b"]}   # what the server expects
groups=a&groups=b      =>  {"groups"=>"b"}          # what this client sends -> only "b" kept

Expected

Multiple groups should all be attached, i.e. the client should emit groups[]=a&groups[]=b, matching the official Java client (TestingbotREST.java), which does:

if (entry.getKey().equals("groups")) {
    for (int i = 0; i < ((List) entry.getValue()).size(); i++) {
        nameValuePairs.add(new BasicNameValuePair("groups[]", ((List) entry.getValue()).get(i).toString()));
    }
}

Suggested fix

Encode array values with []-suffixed repeated keys in flattenObject (or special-case groups), so an array serializes to groups[]=a&groups[]=b.

Note

The server now accepts both a comma-separated string and an array for groups, so single-group calls are unaffected. This issue only impacts calls that pass 2+ groups.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions