Skip to content

Commit b10e237

Browse files
committed
feat(coap-core): add payload methods to CoapRequest builder for byte arrays
1 parent 4a7719b commit b10e237

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
2+
* Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap)
33
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -260,6 +260,14 @@ public Builder payload(Opaque payload) {
260260
return this;
261261
}
262262

263+
public Builder payload(byte[] payload) {
264+
return payload(Opaque.of(payload));
265+
}
266+
267+
public Builder payload(byte[] payload, short contentFormat) {
268+
return payload(Opaque.of(payload), contentFormat);
269+
}
270+
263271
public Builder token(Opaque token) {
264272
this.token = token;
265273
return this;

coap-core/src/test/java/com/mbed/coap/packet/CoapRequestTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
2+
* Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap)
33
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,7 @@
2222
import static com.mbed.coap.packet.CoapRequest.fetch;
2323
import static com.mbed.coap.packet.CoapRequest.get;
2424
import static com.mbed.coap.packet.CoapRequest.ping;
25+
import static com.mbed.coap.packet.CoapRequest.post;
2526
import static com.mbed.coap.packet.CoapResponseTest.newOptions;
2627
import static com.mbed.coap.packet.MediaTypes.CT_APPLICATION_JSON;
2728
import static com.mbed.coap.packet.Opaque.EMPTY;
@@ -81,7 +82,7 @@ public void shouldModifyCoapRequest() {
8182
@Test
8283
void testToString() {
8384
assertEquals("CoapRequest[PUT URI:/test,Token:03ff, pl(4):64757061]", CoapRequest.put("/test").token(1023).payload("dupa").build().toString());
84-
assertEquals("CoapRequest[POST URI:/test, pl(4):64757061]", CoapRequest.post("/test").payload("dupa").build().toString());
85+
assertEquals("CoapRequest[POST URI:/test, pl(4):64757061]", post("/test").payload("dupa").build().toString());
8586
assertEquals("CoapRequest[DELETE URI:/test,Token:03ff]", CoapRequest.delete("/test").token(1023).build().toString());
8687
assertEquals("CoapRequest[GET URI:/test]", get("/test").build().toString());
8788
assertEquals("CoapRequest[FETCH URI:/test, pl(4):64757061]", fetch("/test").payload("dupa").build().toString());
@@ -173,6 +174,23 @@ public void shouldSetBlockOptionBasedOnMethod() {
173174
assertNull(req2.options().getBlock1Req());
174175
}
175176

177+
@Test
178+
public void shouldSetPayloadFromByteArray() {
179+
byte[] data = new byte[]{1, 2, 3, 4};
180+
181+
CoapRequest req = post("/test").payload(data).build();
182+
assertEquals(Opaque.of(data), req.getPayload());
183+
}
184+
185+
@Test
186+
public void shouldSetPayloadFromByteArrayWithContentFormat() {
187+
byte[] data = new byte[]{1, 2, 3, 4};
188+
189+
CoapRequest req = post("/test").payload(data, CT_APPLICATION_JSON).build();
190+
assertEquals(Opaque.of(data), req.getPayload());
191+
assertEquals(CT_APPLICATION_JSON, req.options().getContentFormat());
192+
}
193+
176194
@Test
177195
public void shouldSetObserveOption() {
178196
assertEquals(0, get("/test").observe().build().options().getObserve());

0 commit comments

Comments
 (0)