forked from LianjiaTech/retrofit-spring-boot-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
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
d4454a3
commit cad3089
Showing
11 changed files
with
174 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
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
77 changes: 77 additions & 0 deletions
77
src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/InterceptTest.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.github.lianjiatech.retrofit.spring.boot.test; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.http.InterceptApi; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* @author 陈添明 | ||
* @summary | ||
* @since 2022/1/21 4:20 下午 | ||
*/ | ||
@SpringBootTest(classes = RetrofitTestApplication.class) | ||
@RunWith(SpringRunner.class) | ||
public class InterceptTest { | ||
|
||
@Autowired | ||
private InterceptApi interceptApi; | ||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
|
||
|
||
private MockWebServer server; | ||
|
||
@Before | ||
public void before() throws IOException { | ||
System.out.println("=========开启MockWebServer==========="); | ||
server = new MockWebServer(); | ||
server.start(8080); | ||
|
||
} | ||
|
||
@After | ||
public void after() throws IOException { | ||
System.out.println("=========关闭MockWebServer==========="); | ||
server.close(); | ||
} | ||
|
||
@Test | ||
public void test() throws IOException { | ||
// mock | ||
Person mockPerson = new Person().setId(1L) | ||
.setName("test") | ||
.setAge(10); | ||
Result mockResult = new Result<>() | ||
.setCode(0) | ||
.setMsg("ok") | ||
.setData(mockPerson); | ||
MockResponse response = new MockResponse() | ||
.setResponseCode(200) | ||
.addHeader("Content-Type", "application/json; charset=utf-8") | ||
.addHeader("Cache-Control", "no-cache") | ||
.setBody(objectMapper.writeValueAsString(mockResult)); | ||
server.enqueue(response); | ||
|
||
Result<Person> person = interceptApi.getPerson(1L); | ||
Person data = person.getData(); | ||
Assert.assertNotNull(data); | ||
Assert.assertEquals("test", data.getName()); | ||
Assert.assertEquals(10, data.getAge().intValue()); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/InterceptApi.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,28 @@ | ||
package com.github.lianjiatech.retrofit.spring.boot.test.http; | ||
|
||
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; | ||
import retrofit2.http.GET; | ||
import retrofit2.http.Query; | ||
|
||
/** | ||
* @author 陈添明 | ||
* @summary | ||
* @since 2022/1/21 4:19 下午 | ||
*/ | ||
@RetrofitClient(baseUrl = "${test.baseUrl}") | ||
public interface InterceptApi { | ||
|
||
|
||
/** | ||
* 其他任意Java类型 <br> | ||
* 将响应体内容适配成一个对应的Java类型对象返回,如果http状态码不是2xx,直接抛错!<br> | ||
* | ||
* @param id id | ||
* @return 其他任意Java类型 | ||
*/ | ||
@GET("person") | ||
Result<Person> getPerson(@Query("id") Long id); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
.../com/github/lianjiatech/retrofit/spring/boot/test/interceptor/CustomRetryInterceptor.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,34 @@ | ||
package com.github.lianjiatech.retrofit.spring.boot.test.interceptor; | ||
|
||
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor; | ||
import com.github.lianjiatech.retrofit.spring.boot.retry.RetryRule; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.service.TestService; | ||
import okhttp3.Response; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* @author 陈添明 | ||
* @summary | ||
* @since 2022/1/21 4:52 下午 | ||
*/ | ||
@Component | ||
public class CustomRetryInterceptor extends BaseRetryInterceptor { | ||
|
||
@Autowired | ||
private TestService testService; | ||
|
||
@Override | ||
protected Response retryIntercept(int maxRetries, int intervalMs, RetryRule[] retryRules, Chain chain) { | ||
System.out.println("=============执行重试============="); | ||
testService.test(); | ||
try { | ||
return chain.proceed(chain.request()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
12 changes: 10 additions & 2 deletions
12
...t/test/interceptor/SourceInterceptor.java → .../interceptor/SourceGlobalInterceptor.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 |
---|---|---|
@@ -1,20 +1,28 @@ | ||
package com.github.lianjiatech.retrofit.spring.boot.test.interceptor; | ||
|
||
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseGlobalInterceptor; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.service.TestService; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class SourceInterceptor extends BaseGlobalInterceptor { | ||
public class SourceGlobalInterceptor extends BaseGlobalInterceptor { | ||
|
||
@Autowired | ||
private TestService testService; | ||
|
||
@Override | ||
public Response doIntercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
Request newReq = request.newBuilder() | ||
.addHeader("source", "test") | ||
.build(); | ||
System.out.println("===========执行全局重试==========="); | ||
testService.test(); | ||
return chain.proceed(newReq); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/service/TestService.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,16 @@ | ||
package com.github.lianjiatech.retrofit.spring.boot.test.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author 陈添明 | ||
* @summary | ||
* @since 2022/1/21 4:29 下午 | ||
*/ | ||
@Service | ||
public class TestService { | ||
|
||
|
||
public void test() { | ||
} | ||
} |