-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adds unit tests for HttpRequestUtil.java
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
eventmesh-runtime/src/test/java/org/apache/eventmesh/runtime/util/HttpRequestUtilTest.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,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.eventmesh.runtime.util; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import io.netty.handler.codec.http.DefaultFullHttpRequest; | ||
import io.netty.handler.codec.http.HttpMethod; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
|
||
public class HttpRequestUtilTest { | ||
|
||
@Test | ||
public void testShouldParseHttpGETRequestBody() throws IOException { | ||
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some-path?q1=xyz"); | ||
Map<String, Object> expected = new HashMap<>(); | ||
expected.put("q1", "xyz"); | ||
Assertions.assertEquals(expected, HttpRequestUtil.parseHttpRequestBody(httpRequest)); | ||
} | ||
|
||
@Test | ||
public void testShouldParseHttpPOSTRequestBody() throws IOException { | ||
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/some-path", Unpooled.copiedBuffer(("q1=xyz").getBytes())); | ||
Map<String, Object> expected = new HashMap<>(); | ||
expected.put("q1", "xyz"); | ||
Assertions.assertEquals(expected, HttpRequestUtil.parseHttpRequestBody(httpRequest)); | ||
} | ||
|
||
@Test | ||
public void testQueryStringToMap() { | ||
Map<String, Object> expected = new HashMap<>(); | ||
expected.put("q1", "xyz"); | ||
expected.put("q2", "abc"); | ||
Assertions.assertEquals(expected, HttpRequestUtil.queryStringToMap("q1=xyz&q2=abc")); | ||
} | ||
|
||
@Test | ||
public void testGetQueryParam() { | ||
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some-path?q1=xyz"); | ||
Assertions.assertEquals("xyz", HttpRequestUtil.getQueryParam(httpRequest, "q1", "")); | ||
} | ||
|
||
@Test | ||
public void testGetBodyParam() throws IOException { | ||
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/some-path", Unpooled.copiedBuffer(("q1=xyz").getBytes())); | ||
Assertions.assertEquals("xyz", HttpRequestUtil.getBodyParam(httpRequest, "q1")); | ||
} | ||
|
||
} |