版本: rswag (2.0.5),rspec (3.8.0)
环境: Rails 5.2.3,Ruby2.4.5
这是我第一次使用它,被卡在授权头上一天。以下是我所做的:
# in spec/swagger_helper.rb
config.swagger_docs = {
'api/v1/swagger.json' => {
swagger: '2.0',
info: {
title: 'API V1',
version: 'v1'
},
paths: {},
securityDefinitions: {
JWT: {
description: 'the jwt for API auth',
type: :apiKey,
name: 'Authorization',
in: :header
}
}
}
}# in spec/integration/api/v1/nodes_spec.rb
path '/api/v1/nodes' do
get 'Get all servers' do
tags TAGS_NODE
produces 'application/json'
security [JWT: {}]
parameter name: :searchString, in: :query, type: :string
parameter name: :searchColumn, in: :query, type: :string
#parameter name: 'Authorization', :in => :header, :type => :string
let(:nodes) { create_list(:node_list, 32) }
response '200', 'Servers found' do
let(:'Authorization') { "Bearer #{gen_jwt}" }
let(:searchString) { 'test' }
let(:searchColumn) { ';Name;' }
run_test! do |repsonse|
data = JSON.parse(response.body)
puts data
end
end
end
end预期:“承担者.”设置在“授权”标题中:在测试日志中,我发现:
公司名称:"/api/v1/nodes?searchString=test&searchColumn=;Name;¶ms&headersHTTP_AUTHORIZATION=Bearer+eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1Njk3NjI2NzMsImVtYWlsIjoidGVzdEBpYm0uY29tIiwib3JnYW5pemF0aW9ucyI6WyJ0ZXN0X29yZzEiLCJvcmcyIl0sInJvbGVzIjpbImNjX2NvbnNvbGVfYWRtaW4iLCJyb2xlMiJdLCJpbnZlbnRvcnkiOlsidGVzdF9pbnYiXX0.eenTMWUy6kSHO58_kLKoKWmNjvQ9i5TU9ex4Ou-ausE&headersHTTP_ACCEPT=application%2Fjson“.用JWT令牌调试无reqAuth .错误,没有req,没有JWT令牌包括在请求中
在日志中标记为粗体的“授权”和“接受”标头出现在查询参数中,这些参数应该是http标头,因此在代码中不能从标头中检索JWT令牌。
我还尝试不使用securityDefinition,但是在标题中指定一个参数如下:参数名称:“授权”,:在=> :header中,:键入=> :string。这也没用。
不确定我错过了什么配置,或者我做错了什么?谢谢!
更新:它似乎与其他宝石冲突有关?我又一次尝试创建一个新的Rails 5APIOnly应用程序,只添加rspec和rswag,然后用一个简单的测试用例运行,它成功了!这是我的个人档案:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.4.5'
gem 'rails', '5.2.3'
gem 'puma', '3.11'
gem 'bootsnap', '1.1.0', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
end
group :test do
# Test framework
gem "rspec-rails"
gem "database_cleaner", '1.6.0'
gem "simplecov"
gem "simplecov-rcov"
gem "factory_bot_rails", '5.1.0'
gem "ci_reporter_rspec"
gem "faker"
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'pg', '1.1.4'
gem 'delayed_job_active_record', '4.1.3'
gem 'delayed_job_worker_pool', '0.2.3'
gem 'dalli', '2.7.8'
gem 'ruby-kafka', '0.7.5'
gem 'active_model_serializers', '0.10.10'
gem 'will_paginate', '3.1.7'
gem 'rest-client', '1.8.0'
gem 'symmetric-encryption', '4.3.0', require: false
gem 'unicorn', '5.2.0'
gem 'rubyzip', '1.2.2'
gem 'jwt', '2.2.1'
gem 'rubyXL', '3.3.30'
gem 'apartment', '2.2.1'
gem 'rswag', '2.0.5'已解析的似乎无法使用Rack::Test::方法
它在删除helper文件中的'include::test::Method‘行之后工作,之前添加该文件是为了使用'get’测试API。
发布于 2019-09-29 06:08:14
似乎没有使用Rack::Test::方法
它在删除helper文件中的'include::test::Method‘行之后工作,之前添加该文件是为了使用'get’测试API。
https://stackoverflow.com/questions/58152075
复制相似问题