我在使用Gradle,Groovy和Junit为Cucumber功能步骤定义实现HttpBuilder-NG基本身份验证时遇到问题。我已经使用Behat / PHP成功实现了这个步骤定义。我还使用Postman验证了测试。
这是build.gradle文件
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
compile 'io.github.http-builder-ng:http-builder-ng-core:1.0.2'
testCompile 'junit:junit:4.12'
testCompile 'info.cukes:cucumber-groovy:1.2.5'
testCompile 'info.cukes:cucumber-junit:1.2.5'
}
github API / user / repos路径需要身份验证才能检索用户的存储库信息,但Get正在返回unAuthorized异常。如果我遗漏了路径,我会获得成功,但基本URL不需要身份验证。这是Groovy代码:
import static cucumber.api.groovy.EN.*
import cucumber.api.PendingException
import static groovyx.net.http.HttpBuilder.configure
import static groovyx.net.http.util.SslUtils.ignoreSslIssues
Given(~/^I am an authenticated user$/) { ->
def github = configure {
ignoreSslIssues execution
request.uri = 'https://api.github.com'
request.auth.basic('githubUser', 'githubPassword', false)
}.get {
request.uri.path = '/user/repos'
}
assert github != null
println github.dump()
}
这是我得到的例外(401):
groovyx.net.http.HttpException: Unauthorized
at groovyx.net.http.NativeHandlers.failure(NativeHandlers.java:69)
at groovyx.net.http.HttpConfigs$BaseHttpConfig$$Lambda$9/15235276.apply(Unknown Source)
at groovyx.net.http.HttpBuilder$ResponseHandlerFunction.apply(HttpBuilder.java:2305)
at groovyx.net.http.JavaHttpBuilder$Action.lambda$execute$2(JavaHttpBuilder.java:168)
at groovyx.net.http.JavaHttpBuilder$Action$$Lambda$56/33475769.call(Unknown Source)
at groovyx.net.http.JavaHttpBuilder$ThreadLocalAuth.with(JavaHttpBuilder.java:331)
at groovyx.net.http.JavaHttpBuilder$Action.execute(JavaHttpBuilder.java:122)
at groovyx.net.http.JavaHttpBuilder.createAndExecute(JavaHttpBuilder.java:374)
at groovyx.net.http.JavaHttpBuilder.doGet(JavaHttpBuilder.java:381)
at groovyx.net.http.HttpBuilder$$Lambda$25/32560218.apply(Unknown Source)
at groovyx.net.http.HttpObjectConfigImpl.nullInterceptor(HttpObjectConfigImpl.java:47)
at groovyx.net.http.HttpObjectConfigImpl$Exec$$Lambda$23/7279823.apply(Unknown Source)
at groovyx.net.http.HttpBuilder.get(HttpBuilder.java:346)
Gradle Test Executor 191 finished executing tests.
at groovyx.net.http.HttpBuilder.get(HttpBuilder.java:1297)
at groovyx.net.http.HttpBuilder$get$0.call(Unknown Source)
at repo-create_steps$_run_closure1.doCall(repo-create_steps.groovy:7)
at ?.Given I am an authenticated user(repo-create.feature:3)
答案 0 :(得分:0)
看起来GitHub确实提供了BASIC支持(https://developer.github.com/v3/auth/),但它是非标准的,他们建议您自己创建Authorization
标题,如下所示:
@Grab('io.github.http-builder-ng:http-builder-ng-core:1.0.2')
import static groovyx.net.http.HttpBuilder.configure
import static groovyx.net.http.util.SslUtils.ignoreSslIssues
def username = 'blah'
def password = 'blah'
def creds = "$username:$password".bytes.encodeBase64()
def github = configure {
ignoreSslIssues execution
request.uri = 'https://api.github.com'
request.headers['Authorization'] = "Basic $creds"
}.get {
request.uri.path = '/user/repos'
response.failure { fs, obj->
println "Status: ${fs.statusCode}"
fs.headers.each { h->
println h
}
}
}
println github.dump()
然而,这会带来一个你可能没有的问题。我在我的帐户上启用了双因素身份验证,因此我获得了X-GitHub-OTP: required; :2fa-type
标题(根据上面链接的文档)。如果你没有2因素,你应该得到你需要的东西。
我添加了failure
处理程序以获取有关失败案例的其他信息 - 解决方案不需要它。