如何使用 Fancordion 验证 RESTful 服务 returns HTTP 404?
How to verify a RESTful service that returns a HTTP 404, using Fancordion?
我正在 Fantom programming language. I'm using Fancordion 中编写 RESTful API 来编写验收测试并有以下场景:
Country.fandoc
Country by Code
###############
Passing a country code as a parameter to the Country RESTful service must return the corresponding country
Example
-------
- Given the URL to get the details of Andorra [/country/AD]`set:countryUrl`
- When I send a HTTP GET request to it
- Then I expect the name to be [Andorra]`verifyEq:countryByCode.name`.
CountryFixture.fan
using afBedSheet
using afBounce
using afButter
using afFancordion
using util
@Fixture { specification=`specs/Country.fandoc` }
class CountryFixture : BaseFixture {
Str? countryUrl
Country countryByCode() {
server := BedServer(AppModule#).startup
client := server.makeClient
response := client.sendRequest(ButterRequest(`$countryUrl`)).asStr
Str:Obj? json := JsonInStream(response.in).readJson
country := Country.fromJson(json)
return country
}
}
这很好用。现在我想验证当一个无效的国家代码被传递给请求时,RESTful 服务 returns 一个 HTTP 404 错误,正如我已经使用浏览器实现和验证的那样。但是,我没有在 Fancordion 文档中找到如何验证是否返回了 HTTP 404 错误。我得到的是夹具故障。
***
*** 1 Fixtures FAILED [6 Fixtures]
***
我对这个场景的验收测试是(附加到 Country.fandoc):
Wrong Country Code
##################
Passing a wrong country code as a parameter to the Country RESTful service must cause it to return a HTTP 404 error
Example
-------
- Given the URL to get the details of country that does not exist [/country/XX]`set:countryUrl`
- When I send a HTTP GET request to it
- Then I expect a HTTP 404 error when I try `verifyEq:countryByCode`
请问我怎样才能赶上 404?
如果您正在寻找类似于 verifyErr(Type errType)
方法的东西,Fancordion 没有。那是因为这些方法与并不真正适合 Fancordion 测试风格的实现紧密耦合。
相反,禁用 Butter 引发的 404 错误:
client.errOn4xx.enabled = false
并让 Fancordion 装置设置响应。
Uri? url
ButterResponse? response
Void httpGet() {
client := BedServer(AppModule#).startup.makeClient
client.errOn4xx.enabled = false
this.response = client.get(url)
}
然后让Fancordion规范验证状态码:
Example
-------
- Given the URL to Unknown is [/country/XX]`set:url`
- When I send a [HTTP GET request]`exe:httpGet`
- Then I expect the returned status code to be [404]`eq:response.statusCode`.
我正在 Fantom programming language. I'm using Fancordion 中编写 RESTful API 来编写验收测试并有以下场景:
Country.fandoc
Country by Code
###############
Passing a country code as a parameter to the Country RESTful service must return the corresponding country
Example
-------
- Given the URL to get the details of Andorra [/country/AD]`set:countryUrl`
- When I send a HTTP GET request to it
- Then I expect the name to be [Andorra]`verifyEq:countryByCode.name`.
CountryFixture.fan
using afBedSheet
using afBounce
using afButter
using afFancordion
using util
@Fixture { specification=`specs/Country.fandoc` }
class CountryFixture : BaseFixture {
Str? countryUrl
Country countryByCode() {
server := BedServer(AppModule#).startup
client := server.makeClient
response := client.sendRequest(ButterRequest(`$countryUrl`)).asStr
Str:Obj? json := JsonInStream(response.in).readJson
country := Country.fromJson(json)
return country
}
}
这很好用。现在我想验证当一个无效的国家代码被传递给请求时,RESTful 服务 returns 一个 HTTP 404 错误,正如我已经使用浏览器实现和验证的那样。但是,我没有在 Fancordion 文档中找到如何验证是否返回了 HTTP 404 错误。我得到的是夹具故障。
***
*** 1 Fixtures FAILED [6 Fixtures]
***
我对这个场景的验收测试是(附加到 Country.fandoc):
Wrong Country Code
##################
Passing a wrong country code as a parameter to the Country RESTful service must cause it to return a HTTP 404 error
Example
-------
- Given the URL to get the details of country that does not exist [/country/XX]`set:countryUrl`
- When I send a HTTP GET request to it
- Then I expect a HTTP 404 error when I try `verifyEq:countryByCode`
请问我怎样才能赶上 404?
如果您正在寻找类似于 verifyErr(Type errType)
方法的东西,Fancordion 没有。那是因为这些方法与并不真正适合 Fancordion 测试风格的实现紧密耦合。
相反,禁用 Butter 引发的 404 错误:
client.errOn4xx.enabled = false
并让 Fancordion 装置设置响应。
Uri? url
ButterResponse? response
Void httpGet() {
client := BedServer(AppModule#).startup.makeClient
client.errOn4xx.enabled = false
this.response = client.get(url)
}
然后让Fancordion规范验证状态码:
Example
-------
- Given the URL to Unknown is [/country/XX]`set:url`
- When I send a [HTTP GET request]`exe:httpGet`
- Then I expect the returned status code to be [404]`eq:response.statusCode`.