-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e923e16
commit 2b27382
Showing
19 changed files
with
458 additions
and
10 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
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
17 changes: 17 additions & 0 deletions
17
forest-solon-plugin/src/test/java/com/dtflys/forest/solon/test/sse/MySSEClient.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,17 @@ | ||
package com.dtflys.forest.solon.test.sse; | ||
|
||
import com.dtflys.forest.annotation.BaseRequest; | ||
import com.dtflys.forest.annotation.ForestClient; | ||
import com.dtflys.forest.annotation.Get; | ||
import com.dtflys.forest.http.ForestSSE; | ||
|
||
@ForestClient | ||
@BaseRequest(baseURL = "http://localhost:{port}") | ||
public interface MySSEClient { | ||
|
||
@Get("/sse") | ||
ForestSSE testSSE(); | ||
|
||
@Get(url = "/sse", interceptor = MySSEInterceptor.class) | ||
ForestSSE testSSE_withInterceptor(); | ||
} |
37 changes: 37 additions & 0 deletions
37
forest-solon-plugin/src/test/java/com/dtflys/forest/solon/test/sse/MySSEInterceptor.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,37 @@ | ||
package com.dtflys.forest.solon.test.sse; | ||
|
||
import com.dtflys.forest.annotation.SSEDataMessage; | ||
import com.dtflys.forest.annotation.SSEName; | ||
import com.dtflys.forest.annotation.SSEValue; | ||
import com.dtflys.forest.http.ForestRequest; | ||
import com.dtflys.forest.http.ForestResponse; | ||
import com.dtflys.forest.interceptor.SSEInterceptor; | ||
import org.noear.solon.annotation.Component; | ||
import org.noear.solon.annotation.Inject; | ||
|
||
import java.io.InputStream; | ||
|
||
@Component | ||
public class MySSEInterceptor implements SSEInterceptor { | ||
|
||
@Inject | ||
private TestComp testComp; | ||
|
||
@Override | ||
public void onSuccess(InputStream data, ForestRequest request, ForestResponse response) { | ||
StringBuilder builder = (StringBuilder) request.getOrAddAttachment("text", StringBuilder::new); | ||
builder.append("onSuccess\n"); | ||
} | ||
|
||
@Override | ||
public void afterExecute(ForestRequest request, ForestResponse response) { | ||
StringBuilder builder = (StringBuilder) request.getOrAddAttachment("text", StringBuilder::new); | ||
builder.append("afterExecute\n"); | ||
} | ||
|
||
@SSEDataMessage | ||
public void onData(ForestRequest request, @SSEName String name, @SSEValue String value) { | ||
StringBuilder builder = (StringBuilder) request.getOrAddAttachment("text", StringBuilder::new); | ||
builder.append("Receive name=" + name + "; value=" + value + "; comp=" + testComp.getValue() + "\n"); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
forest-solon-plugin/src/test/java/com/dtflys/forest/solon/test/sse/TestComp.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,12 @@ | ||
package com.dtflys.forest.solon.test.sse; | ||
|
||
|
||
import org.noear.solon.annotation.Component; | ||
|
||
@Component | ||
public class TestComp { | ||
|
||
public String getValue() { | ||
return "test"; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
forest-solon-plugin/src/test/java/com/dtflys/forest/solon/test/sse/TestSSE.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,70 @@ | ||
package com.dtflys.forest.solon.test.sse; | ||
|
||
import com.dtflys.forest.annotation.BindingVar; | ||
import com.dtflys.forest.http.ForestSSE; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.noear.solon.annotation.Inject; | ||
import org.noear.solon.test.SolonJUnit4ClassRunner; | ||
import org.noear.solon.test.SolonTest; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@RunWith(SolonJUnit4ClassRunner.class) | ||
@SolonTest(env = "sse") | ||
public class TestSSE { | ||
|
||
@Rule | ||
public MockWebServer server = new MockWebServer(); | ||
|
||
@Inject | ||
private MySSEClient sseClient; | ||
|
||
@BindingVar("port") | ||
public Integer getPort() { | ||
return server.getPort(); | ||
} | ||
|
||
@Test | ||
public void testSSE() { | ||
server.enqueue(new MockResponse().setResponseCode(200).setBody( | ||
"data:start\n" + | ||
"data:hello\n" | ||
)); | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
|
||
sseClient.testSSE() | ||
.addOnData((eventSource, name, value) -> { | ||
builder.append("Receive ").append(name).append(": ").append(value).append("\n"); | ||
}) | ||
.listen(); | ||
|
||
assertThat(builder.toString()).isEqualTo( | ||
"Receive data: start\n" + | ||
"Receive data: hello\n" | ||
); | ||
} | ||
|
||
|
||
@Test | ||
public void testSSE_withInterceptor() { | ||
server.enqueue(new MockResponse().setResponseCode(200).setBody( | ||
"data:start\n" + | ||
"data:hello\n" | ||
)); | ||
|
||
ForestSSE sse = sseClient.testSSE_withInterceptor().listen(); | ||
|
||
assertThat(sse.getRequest().getAttachment("text").toString()).isEqualTo( | ||
"onSuccess\n" + | ||
"afterExecute\n" + | ||
"Receive name=data; value=start; comp=test\n" + | ||
"Receive name=data; value=hello; comp=test\n" | ||
); | ||
} | ||
|
||
} |
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,7 @@ | ||
|
||
forest: | ||
enabled: true | ||
max-connections: 300 | ||
max-route-connections: 300 | ||
connect-timeout: 3000 | ||
max-retry-count: 0 |
15 changes: 15 additions & 0 deletions
15
...est-spring-boot-test/src/test/java/com/dtflys/forest/springboot/test/sse/MySSEClient.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,15 @@ | ||
package com.dtflys.forest.springboot.test.sse; | ||
|
||
import com.dtflys.forest.annotation.BaseRequest; | ||
import com.dtflys.forest.annotation.Get; | ||
import com.dtflys.forest.http.ForestSSE; | ||
|
||
@BaseRequest(baseURL = "http://localhost:{port}") | ||
public interface MySSEClient { | ||
|
||
@Get("/sse") | ||
ForestSSE testSSE(); | ||
|
||
@Get(url = "/sse", interceptor = MySSEInterceptor.class) | ||
ForestSSE testSSE_withInterceptor(); | ||
} |
37 changes: 37 additions & 0 deletions
37
...pring-boot-test/src/test/java/com/dtflys/forest/springboot/test/sse/MySSEInterceptor.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,37 @@ | ||
package com.dtflys.forest.springboot.test.sse; | ||
|
||
import com.dtflys.forest.annotation.SSEDataMessage; | ||
import com.dtflys.forest.annotation.SSEName; | ||
import com.dtflys.forest.annotation.SSEValue; | ||
import com.dtflys.forest.http.ForestRequest; | ||
import com.dtflys.forest.http.ForestResponse; | ||
import com.dtflys.forest.interceptor.SSEInterceptor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.Resource; | ||
import java.io.InputStream; | ||
|
||
@Component | ||
public class MySSEInterceptor implements SSEInterceptor { | ||
|
||
@Resource | ||
private TestComp testComp; | ||
|
||
@Override | ||
public void onSuccess(InputStream data, ForestRequest request, ForestResponse response) { | ||
StringBuilder builder = (StringBuilder) request.getOrAddAttachment("text", StringBuilder::new); | ||
builder.append("onSuccess\n"); | ||
} | ||
|
||
@Override | ||
public void afterExecute(ForestRequest request, ForestResponse response) { | ||
StringBuilder builder = (StringBuilder) request.getOrAddAttachment("text", StringBuilder::new); | ||
builder.append("afterExecute\n"); | ||
} | ||
|
||
@SSEDataMessage | ||
public void onData(ForestRequest request, @SSEName String name, @SSEValue String value) { | ||
StringBuilder builder = (StringBuilder) request.getOrAddAttachment("text", StringBuilder::new); | ||
builder.append("Receive name=" + name + "; value=" + value + "; comp=" + testComp.getValue() + "\n"); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...forest-spring-boot-test/src/test/java/com/dtflys/forest/springboot/test/sse/TestComp.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,12 @@ | ||
package com.dtflys.forest.springboot.test.sse; | ||
|
||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TestComp { | ||
|
||
public String getValue() { | ||
return "test"; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
.../forest-spring-boot-test/src/test/java/com/dtflys/forest/springboot/test/sse/TestSSE.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,77 @@ | ||
package com.dtflys.forest.springboot.test.sse; | ||
|
||
import com.dtflys.forest.annotation.BindingVar; | ||
import com.dtflys.forest.http.ForestSSE; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ActiveProfiles("sse") | ||
@SpringBootTest(classes = TestSSE.class) | ||
@EnableAutoConfiguration | ||
@ComponentScan(basePackages = {"com.dtflys.forest.springboot.test.sse"}) | ||
public class TestSSE { | ||
|
||
@Rule | ||
public MockWebServer server = new MockWebServer(); | ||
|
||
@Resource | ||
private MySSEClient sseClient; | ||
|
||
@BindingVar | ||
public Integer getPort() { | ||
return server.getPort(); | ||
} | ||
|
||
@Test | ||
public void testSSE() { | ||
server.enqueue(new MockResponse().setResponseCode(200).setBody( | ||
"data:start\n" + | ||
"data:hello\n" | ||
)); | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
|
||
sseClient.testSSE() | ||
.addOnData((eventSource, name, value) -> { | ||
builder.append("Receive ").append(name).append(": ").append(value).append("\n"); | ||
}) | ||
.listen(); | ||
|
||
assertThat(builder.toString()).isEqualTo( | ||
"Receive data: start\n" + | ||
"Receive data: hello\n" | ||
); | ||
} | ||
|
||
|
||
@Test | ||
public void testSSE_withInterceptor() { | ||
server.enqueue(new MockResponse().setResponseCode(200).setBody( | ||
"data:start\n" + | ||
"data:hello\n" | ||
)); | ||
|
||
ForestSSE sse = sseClient.testSSE_withInterceptor().listen(); | ||
|
||
assertThat(sse.getRequest().getAttachment("text").toString()).isEqualTo( | ||
"onSuccess\n" + | ||
"afterExecute\n" + | ||
"Receive name=data; value=start; comp=test\n" + | ||
"Receive name=data; value=hello; comp=test\n" | ||
); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
forest-test/forest-spring-boot-test/src/test/resources/application-sse.yml
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,7 @@ | ||
|
||
forest: | ||
enabled: true | ||
max-connections: 300 | ||
max-route-connections: 300 | ||
connect-timeout: 3000 | ||
max-retry-count: 0 |
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
15 changes: 15 additions & 0 deletions
15
...t-spring-boot3-test/src/test/java/com/dtflys/forest/springboot3/test/sse/MySSEClient.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,15 @@ | ||
package com.dtflys.forest.springboot3.test.sse; | ||
|
||
import com.dtflys.forest.annotation.BaseRequest; | ||
import com.dtflys.forest.annotation.Get; | ||
import com.dtflys.forest.http.ForestSSE; | ||
|
||
@BaseRequest(baseURL = "http://localhost:{port}") | ||
public interface MySSEClient { | ||
|
||
@Get("/sse") | ||
ForestSSE testSSE(); | ||
|
||
@Get(url = "/sse", interceptor = MySSEInterceptor.class) | ||
ForestSSE testSSE_withInterceptor(); | ||
} |
Oops, something went wrong.