-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase code coverage and reliability
- Loading branch information
Showing
6 changed files
with
110 additions
and
35 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
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
53 changes: 53 additions & 0 deletions
53
...ts/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/MockResponseHandler.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,53 @@ | ||
package com.microsoft.kiota.http.middleware; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.MediaType; | ||
import okhttp3.Protocol; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import okio.Buffer; | ||
|
||
/** | ||
* Returns the request body as the response body | ||
*/ | ||
public class MockResponseHandler implements Interceptor { | ||
private int statusCode; | ||
|
||
public MockResponseHandler(int statusCode) { | ||
this.statusCode = statusCode; | ||
} | ||
|
||
public MockResponseHandler() { | ||
this.statusCode = 200; | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
final var request = chain.request(); | ||
final var requestBody = request.body(); | ||
if (request != null && requestBody != null) { | ||
final var buffer = new Buffer(); | ||
requestBody.writeTo(buffer); | ||
return new Response.Builder() | ||
.code(this.statusCode) | ||
.message("OK") | ||
.protocol(Protocol.HTTP_1_1) | ||
.request(request) | ||
.body( | ||
ResponseBody.create( | ||
buffer.readByteArray(), | ||
MediaType.parse("application/json"))) | ||
.build(); | ||
} | ||
return new Response.Builder() | ||
.code(this.statusCode) | ||
.message("OK") | ||
.protocol(Protocol.HTTP_1_1) | ||
.request(request) | ||
.body(ResponseBody.create("", MediaType.parse("application/json"))) | ||
.build(); | ||
} | ||
|
||
} |