-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to define a route tag for COAP metrics (#60)
* Add route to the metrics filter builder * Add way to wrap COAP routes with a filter * Fix linting * Fix linting
- Loading branch information
1 parent
52b521f
commit a98c39c
Showing
4 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
coap-core/src/test/java/com/mbed/coap/server/RouterServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mbed.coap.server; | ||
|
||
import static com.mbed.coap.packet.CoapResponse.ok; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import com.mbed.coap.packet.CoapRequest; | ||
import com.mbed.coap.packet.CoapResponse; | ||
import com.mbed.coap.packet.Code; | ||
import com.mbed.coap.utils.Service; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class RouterServiceTest { | ||
Service<CoapRequest, CoapResponse> simpleHandler = | ||
(CoapRequest r) -> CompletableFuture.completedFuture(ok(r.getMethod() + " " + r.options().getUriPath())); | ||
|
||
@Test | ||
public void shouldBuildSimpleService() throws ExecutionException, InterruptedException { | ||
Service<CoapRequest, CoapResponse> svc = RouterService.builder() | ||
.get("/test1", simpleHandler) | ||
.post("/test1", simpleHandler) | ||
.get("/test2/*", simpleHandler) | ||
.get("/test3", simpleHandler) | ||
.build(); | ||
|
||
assertEquals("GET /test1", svc.apply(CoapRequest.get("/test1")).get().getPayloadString()); | ||
assertEquals("POST /test1", svc.apply(CoapRequest.post("/test1")).get().getPayloadString()); | ||
assertEquals("GET /test2/prefixed-route", svc.apply(CoapRequest.get("/test2/prefixed-route")).get().getPayloadString()); | ||
assertEquals(Code.C404_NOT_FOUND, svc.apply(CoapRequest.get("/test3/not-prefixed-route")).get().getCode()); | ||
} | ||
|
||
@Test | ||
public void shouldWrapRoutes() throws ExecutionException, InterruptedException { | ||
Service<CoapRequest, CoapResponse> svc = RouterService.builder() | ||
.get("/test1", simpleHandler) | ||
.post("/test1", simpleHandler) | ||
.get("/test2/*", simpleHandler) | ||
.wrapRoutes((CoapRequest req, Service<CoapRequest, CoapResponse> nextSvc) -> CompletableFuture.completedFuture(ok("42"))) | ||
.get("/test3", simpleHandler) | ||
.build(); | ||
|
||
assertEquals("42", svc.apply(CoapRequest.get("/test1")).get().getPayloadString()); | ||
assertEquals("42", svc.apply(CoapRequest.post("/test1")).get().getPayloadString()); | ||
assertEquals("42", svc.apply(CoapRequest.get("/test2/prefixed-route")).get().getPayloadString()); | ||
assertEquals("GET /test3", svc.apply(CoapRequest.get("/test3")).get().getPayloadString()); | ||
assertEquals(Code.C404_NOT_FOUND, svc.apply(CoapRequest.get("/test4")).get().getCode()); | ||
} | ||
|
||
@Test | ||
public void shouldChangeDefaultHandler() throws ExecutionException, InterruptedException { | ||
Service<CoapRequest, CoapResponse> svc = RouterService.builder() | ||
.defaultHandler((CoapRequest r) -> CompletableFuture.completedFuture(ok("OK"))) | ||
.build(); | ||
|
||
assertEquals(Code.C205_CONTENT, svc.apply(CoapRequest.get("/test3")).get().getCode()); | ||
} | ||
|
||
@Test | ||
public void shouldMergeRoutes() throws ExecutionException, InterruptedException { | ||
RouterService.RouteBuilder builder1 = RouterService.builder() | ||
.get("/test1", simpleHandler); | ||
RouterService.RouteBuilder builder2 = RouterService.builder() | ||
.get("/test2", simpleHandler) | ||
.mergeRoutes(builder1); | ||
|
||
Service<CoapRequest, CoapResponse> svc = builder2.build(); | ||
|
||
assertEquals(Code.C205_CONTENT, svc.apply(CoapRequest.get("/test1")).get().getCode()); | ||
assertEquals(Code.C205_CONTENT, svc.apply(CoapRequest.get("/test2")).get().getCode()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters