| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require 'rspec_helper' |
| 4 |
require 'asciidoctor' |
| 5 |
require_relative '../lib/asciidoctor/extensions/asciidoctor_kroki' |
| 6 |
|
| 7 |
describe AsciidoctorExtensions::KrokiClient do |
| 8 |
it 'should use adaptive method when http method is invalid' do |
| 9 |
kroki_http_client = AsciidoctorExtensions::KrokiHttpClient |
| 10 |
kroki_client = AsciidoctorExtensions::KrokiClient.new(server_url: 'http://localhost:8000', http_method: 'patch', http_client: kroki_http_client) |
| 11 |
expect(kroki_client.method).to eq('adaptive') |
| 12 |
end |
| 13 |
it 'should use post method when http method is post' do |
| 14 |
kroki_http_client = AsciidoctorExtensions::KrokiHttpClient |
| 15 |
kroki_client = AsciidoctorExtensions::KrokiClient.new(server_url: 'http://localhost:8000', http_method: 'POST', http_client: kroki_http_client) |
| 16 |
expect(kroki_client.method).to eq('post') |
| 17 |
end |
| 18 |
it 'should use get method when http method is get' do |
| 19 |
kroki_http_client = AsciidoctorExtensions::KrokiHttpClient |
| 20 |
kroki_client = AsciidoctorExtensions::KrokiClient.new(server_url: 'http://localhost:8000', http_method: 'get', http_client: kroki_http_client) |
| 21 |
expect(kroki_client.method).to eq('get') |
| 22 |
end |
| 23 |
it 'should use 4000 as the default max URI length' do |
| 24 |
kroki_http_client = AsciidoctorExtensions::KrokiHttpClient |
| 25 |
kroki_client = AsciidoctorExtensions::KrokiClient.new(server_url: 'http://localhost:8000', http_method: 'get', http_client: kroki_http_client) |
| 26 |
expect(kroki_client.max_uri_length).to eq(4000) |
| 27 |
end |
| 28 |
it 'should use a custom value as max URI length' do |
| 29 |
kroki_http_client = AsciidoctorExtensions::KrokiHttpClient |
| 30 |
kroki_client = AsciidoctorExtensions::KrokiClient.new(server_url: 'http://localhost:8000', http_method: 'get', http_client: kroki_http_client, max_uri_length: 8000) |
| 31 |
expect(kroki_client.max_uri_length).to eq(8000) |
| 32 |
end |
| 33 |
it 'should get an image with POST request if the URI length is greater than the value configured' do |
| 34 |
kroki_http_client = Class.new do |
| 35 |
class << self |
| 36 |
def get(uri, _) |
| 37 |
"GET #{uri}" |
| 38 |
end |
| 39 |
|
| 40 |
def post(uri, data, _, _) |
| 41 |
"POST #{uri} - #{data}" |
| 42 |
end |
| 43 |
end |
| 44 |
end |
| 45 |
kroki_diagram = Class.new do |
| 46 |
attr_reader :type, :text, :format, :opts |
| 47 |
|
| 48 |
def initialize(type, format, text, opts = {}) |
| 49 |
@text = text |
| 50 |
@type = type |
| 51 |
@format = format |
| 52 |
@opts = opts |
| 53 |
end |
| 54 |
|
| 55 |
def get_diagram_uri(_) |
| 56 |
'diagram-uri' |
| 57 |
end |
| 58 |
end.new('type', 'format', 'text') |
| 59 |
kroki_client = AsciidoctorExtensions::KrokiClient.new(server_url: 'http://localhost:8000', http_method: 'adaptive', http_client: kroki_http_client, max_uri_length: 10) |
| 60 |
result = kroki_client.get_image(kroki_diagram, 'utf8') |
| 61 |
expect(result).to eq('POST http://localhost:8000/type/format - text') |
| 62 |
end |
| 63 |
it 'should get an image with GET request if the URI length is lower or equals than the value configured' do |
| 64 |
kroki_http_client = Class.new do |
| 65 |
class << self |
| 66 |
def get(uri, _, _) |
| 67 |
"GET #{uri}" |
| 68 |
end |
| 69 |
|
| 70 |
def post(uri, data, _, _) |
| 71 |
"POST #{uri} - #{data}" |
| 72 |
end |
| 73 |
end |
| 74 |
end |
| 75 |
kroki_diagram = Class.new do |
| 76 |
attr_reader :type, :text, :format, :opts |
| 77 |
|
| 78 |
def initialize(type, format, text, opts = {}) |
| 79 |
@text = text |
| 80 |
@type = type |
| 81 |
@format = format |
| 82 |
@opts = opts |
| 83 |
end |
| 84 |
|
| 85 |
def get_diagram_uri(_) |
| 86 |
'diagram-uri' |
| 87 |
end |
| 88 |
end.new('type', 'format', 'text') |
| 89 |
kroki_client = AsciidoctorExtensions::KrokiClient.new(server_url: 'http://localhost:8000', http_method: 'adaptive', http_client: kroki_http_client, max_uri_length: 11) |
| 90 |
result = kroki_client.get_image(kroki_diagram, 'utf8') |
| 91 |
expect(result).to eq('GET diagram-uri') |
| 92 |
end |
| 93 |
end |