Как проверить сервис RESTful, который возвращает HTTP 404, используя Fancordion?

Я пишу RESTful API на языке программирования Fantom. Я использую Fancordion для написания приемочных тестов и имею такой сценарий:

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 возвращает ошибку 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, пожалуйста?

1 ответ

Решение

Если вы ищете что-то похожее на verifyErr(Type errType) Метод, у Фанкордиона его нет. Это связано с тем, что такие методы тесно связаны с реализацией, которая не совсем соответствует стилю тестов Fancordion.

Вместо этого отключите 404 Err, поднятую Butter с помощью:

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`.
Другие вопросы по тегам