### POST /streaming/account/{{accountId}}/device/ (Register Device)
POST {{host}}/streaming/account/{{accountId}}/device/
Content-Type: application/vnd.bose.streaming-v1.2+xml
Authorization: Bearer {{token}}

<?xml version="1.0" encoding="UTF-8" ?>
<device deviceid="{{deviceId}}">
    <name>{{deviceName}}</name>
    <macaddress>{{macAddress1}}</macaddress>
</device>

> {%
    client.test("Device registered successfully", function() {
        client.assert(response.status === 200 || response.status === 201, "Response status is not 200 or 201");
        client.assert(response.contentType.mimeType === "application/vnd.bose.streaming-v1.2+xml", "Response Content-Type should be application/vnd.bose.streaming-v1.2+xml");

        const doc = response.body;
        const device = doc.getElementsByTagName("device")[0];
        client.assert(device !== undefined, "Response body should contain <device>");
        client.assert(device.getAttribute("deviceid") === client.variables.environment.get("deviceId"), "Response body should contain the deviceId");

        const name = device.getElementsByTagName("name")[0];
        client.assert(name !== undefined, "Response body should contain <name>");
        client.assert(name.textContent === client.variables.environment.get("deviceName"), "name should match requested name");

        const createdOn = device.getElementsByTagName("createdOn")[0];
        client.assert(createdOn !== undefined, "Response body should contain <createdOn>");
        client.assert(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|(\+\d{2}:\d{2}))$/.test(createdOn.textContent), "createdOn should be a valid ISO8601 timestamp");

        const updatedOn = device.getElementsByTagName("updatedOn")[0];
        client.assert(updatedOn !== undefined, "Response body should contain <updatedOn>");
        client.assert(updatedOn.textContent === createdOn.textContent, "updatedOn should match createdOn for a new device");

        const ipaddress = device.getElementsByTagName("ipaddress")[0];
        client.assert(ipaddress !== undefined, "Response body should contain <ipaddress>");

        // Capture the initial createdOn so rename_device.http can
        // assert it survives a PUT later in the test sequence.
        client.global.set("initialCreatedOn", createdOn.textContent);
    });
%}

### POST /streaming/account/{{accountId}}/device/ (Register Device Variant)
POST {{host}}/streaming/account/{{accountId}}/device/
Content-Type: application/vnd.bose.streaming-v1.2+xml
Authorization: Bearer {{token}}

<?xml version="1.0" encoding="UTF-8" ?>
<device deviceid="{{deviceId}}">
    <name>{{deviceName}}</name>
    <macaddress>{{macAddress1}}mac</macaddress>
</device>

> {%
    client.test("Device registered successfully (variant)", function() {
        client.assert(response.status === 200 || response.status === 201, "Response status should be 200 or 201");
        client.assert(response.contentType.mimeType === "application/vnd.bose.streaming-v1.2+xml", "Response Content-Type should be application/vnd.bose.streaming-v1.2+xml");
        client.assert(response.headers.valueOf("Location").includes("/account/" + client.variables.environment.get("accountId") + "/device/" + client.variables.environment.get("deviceId")), "Location header should point to the created device");

        const doc = response.body;
        const device = doc.getElementsByTagName("device")[0];
        client.assert(device !== undefined, "Response body should contain <device>");
        client.assert(device.getAttribute("deviceid") === client.variables.environment.get("deviceId"), "Response body should contain the deviceId");

        const createdOn = device.getElementsByTagName("createdOn")[0];
        client.assert(createdOn !== undefined, "Response body should contain <createdOn>");
        client.assert(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|(\+\d{2}:\d{2}))$/.test(createdOn.textContent), "createdOn should be a valid ISO8601 timestamp");
        // This variant POST re-uses the same deviceId as the first
        // register above, so it's an upsert — issue #285 made the
        // datastore preserve the original createdOn unconditionally.
        // The captured global is the load-bearing assertion: if it
        // ever differs here, CreatedOn preservation regressed.
        client.assert(createdOn.textContent === client.global.get("initialCreatedOn"),
            "createdOn (" + createdOn.textContent + ") differs from the value captured by the first POST (" + client.global.get("initialCreatedOn") + ") — upsert must preserve the original timestamp");

        const name = device.getElementsByTagName("name")[0];
        client.assert(name !== undefined, "Response body should contain <name>");
        client.assert(name.textContent === client.variables.environment.get("deviceName"), "name should match requested name");

        const updatedOn = device.getElementsByTagName("updatedOn")[0];
        client.assert(updatedOn !== undefined, "Response body should contain <updatedOn>");
        client.assert(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|(\+\d{2}:\d{2}))$/.test(updatedOn.textContent), "updatedOn should be a valid ISO8601 timestamp");

        const ipaddress = device.getElementsByTagName("ipaddress")[0];
        client.assert(ipaddress !== undefined, "Response body should contain <ipaddress>");
    });
%}
