Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhuynh27 committed Mar 21, 2021
1 parent 1473808 commit 9c187b9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 40 deletions.
68 changes: 28 additions & 40 deletions core/src/test/java/com/jinyframework/HTTPTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jinyframework;

import lombok.val;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -15,15 +15,15 @@ public abstract class HTTPTest {
protected String url = "";
protected boolean isCI =
System.getenv("CI") != null
&& System.getenv("CI").toLowerCase().equals("true");
&& System.getenv("CI").equalsIgnoreCase("true");

@Test
@DisplayName("Hello World")
void helloWorld() throws IOException {
val res = HttpClient.builder()
.url(url + "/").method("GET")
.build().perform();
assertEquals(res.getBody(), "Hello World");
assertEquals("Hello World", res.getBody());
}

@Test
Expand All @@ -32,7 +32,7 @@ void defaultResponseHeaders() throws IOException {
val res = HttpClient.builder()
.url(url + "/").method("GET")
.build().perform();
assertEquals(res.getHeader("Content-Type"), "application/json");
assertEquals("application/json", res.getHeader("Content-Type"));
}

@Test
Expand All @@ -41,30 +41,26 @@ void immediateReturn() throws IOException {
val res = HttpClient.builder()
.url(url + "/immediate").method("GET")
.build().perform();
assertEquals(res.getBody(), "Foo");
assertEquals("Foo", res.getBody());
}

// Temporary disable: CI error
@Test
@DisplayName("Transformer")
void transformer() throws IOException {
// Reason for failing on CI still not known
if (isCI) return;

val res = HttpClient.builder()
.url(url + "/transform").method("POST").body("transform")
.build().perform();
assertEquals(res.getBody(), "transformed");
assertEquals("transformed", res.getBody());
}

@Test
@DisplayName("Echo")
void echo() throws IOException {
if (isCI) return;

val res = HttpClient.builder()
.url(url + "/echo").method("POST").body("copycat")
.build().perform();
assertEquals(res.getBody(), "copycat");
assertEquals("copycat", res.getBody());
}

@Test
Expand All @@ -73,7 +69,7 @@ void header() throws IOException {
val res = HttpClient.builder()
.url(url + "/header").method("GET")
.build().perform();
assertEquals(res.getHeader("foo"), "bar");
assertEquals("bar", res.getHeader("foo"));
}

@Test
Expand All @@ -83,7 +79,7 @@ void headerParams() throws IOException {
.url(url + "/req-header").method("GET")
.header("Foo", "foo").header("Bar", "bar")
.build().perform();
assertEquals(res.getBody(), "foobar");
assertEquals("foobar", res.getBody());
}

@Test
Expand All @@ -101,7 +97,7 @@ void pathParams() throws IOException {
val res = HttpClient.builder()
.url(url + "/path/hello/123").method("GET")
.build().perform();
assertEquals(res.getBody(), "hello:123");
assertEquals("hello:123", res.getBody());
}

@Test
Expand All @@ -110,7 +106,7 @@ void dataParams() throws IOException {
val res = HttpClient.builder()
.url(url + "/data/param").method("GET")
.build().perform();
assertEquals(res.getBody(), "ok");
assertEquals("ok", res.getBody());
}

@Test
Expand All @@ -119,7 +115,7 @@ void catchAll() throws IOException {
val res = HttpClient.builder()
.url(url + "/all/123/456").method("GET")
.build().perform();
assertEquals(res.getBody(), "/all/123/456");
assertEquals("/all/123/456", res.getBody());
}

@Test
Expand All @@ -140,8 +136,8 @@ void middlewareSuccess() throws IOException {
val res = HttpClient.builder()
.url(url + "/protected").method("GET").headers(headers)
.build().perform();
assertEquals(res.getStatus(), 200);
assertEquals(res.getBody(), "success:tuhuynh");
assertEquals(200, res.getStatus());
assertEquals("success:tuhuynh", res.getBody());
}

@Test
Expand All @@ -150,8 +146,8 @@ void panic() throws IOException {
val res = HttpClient.builder()
.url(url + "/panic").method("GET")
.build().perform();
assertEquals(res.getStatus(), 500);
assertEquals(res.getBody(), "Panicked!");
assertEquals(500, res.getStatus());
assertEquals("Panicked!", res.getBody());
}

@Test
Expand All @@ -160,19 +156,19 @@ void pathCase() throws IOException {
val hasOk = HttpClient.builder()
.url(url + "/hasCase").method("GET")
.build().perform();
assertEquals(200, hasOk.getStatus());
assertEquals(hasOk.getStatus(), 200);
val hasFail = HttpClient.builder()
.url(url + "/hascase").method("GET")
.build().perform();
assertEquals(404, hasFail.getStatus());
assertEquals(hasFail.getStatus(),404);
val nonOk = HttpClient.builder()
.url(url + "/noncase").method("GET")
.build().perform();
assertEquals(200, nonOk.getStatus());
assertEquals(nonOk.getStatus(), 200);
val nonFail = HttpClient.builder()
.url(url + "/nonCase").method("GET")
.build().perform();
assertEquals(404, nonFail.getStatus());
assertEquals(nonFail.getStatus(), 404);
}

@Test
Expand All @@ -181,7 +177,7 @@ void globalMiddleware() throws IOException {
val res = HttpClient.builder()
.url(url + "/gm").method("GET")
.build().perform();
assertEquals(res.getBody(), "middleware");
assertEquals("middleware", res.getBody());
}

@Test
Expand All @@ -190,7 +186,7 @@ void globalMiddlewareNotFromSubRouter() throws IOException {
val res = HttpClient.builder()
.url(url + "/gm-sub").method("GET")
.build().perform();
assertEquals(res.getBody(), "");
assertEquals("", res.getBody());
}

@Test
Expand All @@ -199,19 +195,17 @@ void subRouter1() throws IOException {
val res = HttpClient.builder()
.url(url + "/cat").method("GET")
.build().perform();
assertEquals(res.getBody(), "this is a cat");
assertEquals("this is a cat", res.getBody());
}

@Test
@DisplayName("SubRouter2")
void subRouter2() throws IOException {
if (isCI) return;

val res = HttpClient.builder()
.url(url + "/cat/test").method("POST")
.body("catTest")
.build().perform();
assertEquals(res.getBody(), "catTest");
assertEquals("catTest", res.getBody());
}

@Test
Expand All @@ -229,17 +223,11 @@ void subRouter4() throws IOException {
val res = HttpClient.builder()
.url(url + "/cat/foo/bar").method("GET")
.build().perform();
assertEquals(res.getBody(), "foo:bar");
assertEquals("foo:bar", res.getBody());
}

@BeforeEach
@AfterEach
void each() throws InterruptedException {
// Do something on CI if (isCI)
// TimeUnit.SECONDS.sleep(1);
// No sleep

if (isCI) {
TimeUnit.MILLISECONDS.sleep(1000);
}
TimeUnit.MILLISECONDS.sleep(100);
}
}
47 changes: 47 additions & 0 deletions core/src/test/java/com/jinyframework/LoopTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.jinyframework;

import com.jinyframework.core.AbstractRequestBinder.HttpResponse;
import lombok.val;
import org.junit.jupiter.api.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class LoopTest {
private final String url;
private static final HttpServer server = HttpServer.port(1234);

public LoopTest() {
this.url = "http://localhost:1234";
}

@BeforeAll
static void startServer() throws InterruptedException {
new Thread(() -> {
server.post("/transform", ctx -> HttpResponse.of(ctx.getBody()).transform(s -> s + "ed"));
server.post("/echo", ctx -> HttpResponse.of(ctx.getBody()));
server.start();
}).start();

// Wait for server to start
TimeUnit.SECONDS.sleep(3);
}

@AfterAll
static void stopServer() {
server.stop();
}

@RepeatedTest(10)
@DisplayName("Transformer")
void transformer() throws IOException, InterruptedException {
val res = HttpClient.builder()
.url(url + "/transform").method("POST").body("transform")
.build().perform();
assertEquals("transformed", res.getBody());
// TODO: fix loop error
TimeUnit.MILLISECONDS.sleep(100);
}
}

0 comments on commit 9c187b9

Please sign in to comment.