Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -260,6 +260,14 @@ public Builder payload(Opaque payload) {
return this;
}

public Builder payload(byte[] payload) {
return payload(Opaque.of(payload));
}

public Builder payload(byte[] payload, short contentFormat) {
return payload(Opaque.of(payload), contentFormat);
}

public Builder token(Opaque token) {
this.token = token;
return this;
Expand Down
22 changes: 20 additions & 2 deletions coap-core/src/test/java/com/mbed/coap/packet/CoapRequestTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,6 +22,7 @@
import static com.mbed.coap.packet.CoapRequest.fetch;
import static com.mbed.coap.packet.CoapRequest.get;
import static com.mbed.coap.packet.CoapRequest.ping;
import static com.mbed.coap.packet.CoapRequest.post;
import static com.mbed.coap.packet.CoapResponseTest.newOptions;
import static com.mbed.coap.packet.MediaTypes.CT_APPLICATION_JSON;
import static com.mbed.coap.packet.Opaque.EMPTY;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void shouldModifyCoapRequest() {
@Test
void testToString() {
assertEquals("CoapRequest[PUT URI:/test,Token:03ff, pl(4):64757061]", CoapRequest.put("/test").token(1023).payload("dupa").build().toString());
assertEquals("CoapRequest[POST URI:/test, pl(4):64757061]", CoapRequest.post("/test").payload("dupa").build().toString());
assertEquals("CoapRequest[POST URI:/test, pl(4):64757061]", post("/test").payload("dupa").build().toString());
assertEquals("CoapRequest[DELETE URI:/test,Token:03ff]", CoapRequest.delete("/test").token(1023).build().toString());
assertEquals("CoapRequest[GET URI:/test]", get("/test").build().toString());
assertEquals("CoapRequest[FETCH URI:/test, pl(4):64757061]", fetch("/test").payload("dupa").build().toString());
Expand Down Expand Up @@ -173,6 +174,23 @@ public void shouldSetBlockOptionBasedOnMethod() {
assertNull(req2.options().getBlock1Req());
}

@Test
public void shouldSetPayloadFromByteArray() {
byte[] data = new byte[]{1, 2, 3, 4};

CoapRequest req = post("/test").payload(data).build();
assertEquals(Opaque.of(data), req.getPayload());
}

@Test
public void shouldSetPayloadFromByteArrayWithContentFormat() {
byte[] data = new byte[]{1, 2, 3, 4};

CoapRequest req = post("/test").payload(data, CT_APPLICATION_JSON).build();
assertEquals(Opaque.of(data), req.getPayload());
assertEquals(CT_APPLICATION_JSON, req.options().getContentFormat());
}

@Test
public void shouldSetObserveOption() {
assertEquals(0, get("/test").observe().build().options().getObserve());
Expand Down
Loading