Skip to content

Commit

Permalink
Add characteristic WRITE tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Oct 14, 2022
1 parent c1e7ccf commit f7a3062
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
42 changes: 40 additions & 2 deletions src/lib/HAPServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ describe("HAPServer", () => {
expect(responseJSON).toEqual({ accessories: [] });
});

test("test successful /characteristics", async () => {
test("test successful GET /characteristics", async () => {
server = new HAPServer(accessoryInfoPaired);
const [port, address] = await bindServer(server);

Expand Down Expand Up @@ -437,7 +437,7 @@ describe("HAPServer", () => {
expect(responseJSON).toEqual({ characteristics: readData });
});

test("test /characteristics with errors", async () => {
test("test GET /characteristics with errors", async () => {
server = new HAPServer(accessoryInfoPaired);
const [port, address] = await bindServer(server);

Expand Down Expand Up @@ -494,6 +494,44 @@ describe("HAPServer", () => {
] });
});

test("test successful GET /characteristics value write", async () => {
server = new HAPServer(accessoryInfoPaired);
const [port, address] = await bindServer(server);

const pairVerify = new PairVerifyClient(port, httpAgent);
const encryption = await pairVerify.sendPairVerify(serverInfoPaired, clientInfo);

const client = new HTTPClient(httpAgent, address, port);
client.attachSocket();

server.on(HAPServerEventTypes.SET_CHARACTERISTICS, (connection, request, callback) => {
expect(connection.encryption).not.toBeUndefined();
// TODO validate
callback(undefined, { characteristics: [] }); // TODO response!
});

const httpRequest = client.formatHTTPRequest(
"PUT",
"/characteristics",
Buffer.from(JSON.stringify({

})),
"application/hap+json",
);
client.write(httpRequest, encryption);
await PromiseTimeout(10);

const plaintext = client.popReceiveBuffer(encryption);
const response = client.parseHTTPResponse(plaintext);
expect(response.statusCode).toEqual(HAPHTTPCode.NO_CONTENT);
});

// TODO PUT /characteristic with MULTI_STATUS resposnse!

// TODO test write reponse?

// TODO test illegal json formatting!

test.each(["pairings", "accessories", "characteristics", "prepare", "resource"])(
"request to \"/%s\" should be rejected in unverified state",
async (route: string) => {
Expand Down
7 changes: 4 additions & 3 deletions src/lib/HAPServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -941,11 +941,12 @@ export class HAPServer extends EventEmitter {
}
}
if (data.length === 0) {
response.writeHead(400, { "Content-Type": "application/hap+json" });
response.writeHead(HAPHTTPCode.BAD_REQUEST, { "Content-Type": "application/hap+json" });
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
return;
}

// TODO catch parsing errors?
const writeRequest = JSON.parse(data.toString("utf8")) as CharacteristicsWriteRequest;

this.emit(
Expand All @@ -959,13 +960,13 @@ export class HAPServer extends EventEmitter {
return;
}

// typescript can't type that this exists if error doesnt
// typescript can't type that this exists if error doesn't
const characteristics = writeResponse!.characteristics;

let multiStatus = false;
for (const data of characteristics) {
if (data.status || data.value !== undefined) {
// also send multiStatus on write response requests
// also send multiStatus on write response requests
multiStatus = true;
break;
}
Expand Down

0 comments on commit f7a3062

Please sign in to comment.