Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test postWithHeader and postWithText #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/test/groovy/org/nofdev/http/HttpClientUtilSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ class HttpClientUtilSpec extends Specification {
httpMessage.contentType == "text/html"
}

def "postWithHeader请求测试"() {
setup:
mockServer.when(HttpRequest.request().withURL(url).withHeader(new Header("Accept", "application/json")).withHeader(new Header("Content-Type", "application/json")))
.respond(HttpResponse.response().withStatusCode(200).withHeader(new Header("Content-Type", "text/html")).withBody("hello world"))
def headers = new HashMap()
headers.put("Accept", "application/json")
headers.put("Content-Type", "application/json")
def httpMessage = new HttpClientUtil(new PoolingConnectionManagerFactory()).postWithHeader(url, [:], headers)
expect:
httpMessage.body == "hello world"
httpMessage.statusCode == 200
httpMessage.contentType == "text/html"
}

def "postWithText请求测试"() {
setup:
mockServer.when(HttpRequest.request().withURL(url).withHeader(new Header("Accept", "application/json")).withHeader(new Header("Content-Type", "application/json")))
.respond(HttpResponse.response().withStatusCode(200).withHeader(new Header("Content-Type", "application/json")).withBody("hello world"))
def headers = new HashMap()
headers.put("Accept", "application/json")
headers.put("Content-Type", "application/json")
def httpMessage = new HttpClientUtil(new PoolingConnectionManagerFactory()).postWithText(url, "hello world", headers)
expect:
httpMessage.body == "hello world"
httpMessage.statusCode == 200
httpMessage.contentType == "application/json"
}

def "测试响应超时"() {
setup:
mockServer.when(HttpRequest.request().withURL(url)).respond(HttpResponse.response().withStatusCode(200).withBody("hello world").withDelay(new Delay(TimeUnit.SECONDS, 2)))
Expand Down