-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generator for Ruby's Faraday client (#362)
Co-authored-by: Lukas_Skywalker <[email protected]>
- Loading branch information
1 parent
147eb98
commit 8739d73
Showing
20 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { CodeBuilder } from '../../../helpers/code-builder'; | ||
import { escapeForSingleQuotes } from '../../../helpers/escape'; | ||
import { Client } from '../../targets'; | ||
|
||
export const faraday: Client = { | ||
info: { | ||
key: 'faraday', | ||
title: 'faraday', | ||
link: 'https://github.com/lostisland/faraday', | ||
description: 'Faraday HTTP client', | ||
}, | ||
convert: ({ uriObj, queryObj, method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => { | ||
const { push, blank, join } = new CodeBuilder(); | ||
Check warning on line 13 in src/targets/ruby/faraday/client.ts GitHub Actions / build (16)
Check warning on line 13 in src/targets/ruby/faraday/client.ts GitHub Actions / build (18)
|
||
|
||
Check warning on line 14 in src/targets/ruby/faraday/client.ts GitHub Actions / build (16)
Check warning on line 14 in src/targets/ruby/faraday/client.ts GitHub Actions / build (18)
|
||
// To support custom methods we check for the supported methods | ||
// and if doesn't exist then we build a custom class for it | ||
const method = rawMethod.toUpperCase(); | ||
const methods = [ | ||
'GET', | ||
'POST', | ||
'HEAD', | ||
'DELETE', | ||
'PATCH', | ||
'PUT', | ||
'OPTIONS', | ||
'COPY', | ||
'LOCK', | ||
'UNLOCK', | ||
'MOVE', | ||
'TRACE', | ||
]; | ||
|
||
if(!methods.includes(method)) { | ||
push(`# Faraday cannot currently run ${method} requests. Please use another client.`) | ||
return join(); | ||
} | ||
|
||
push("require 'faraday'"); | ||
blank(); | ||
|
||
// Write body to beginning of script | ||
if(postData.mimeType === 'application/x-www-form-urlencoded') { | ||
if (postData.params) { | ||
push(`data = {`); | ||
postData.params.forEach(param => { | ||
push(` :${param.name} => ${JSON.stringify(param.value)},`); | ||
}); | ||
push(`}`); | ||
blank(); | ||
} | ||
} | ||
|
||
push(`conn = Faraday.new(`); | ||
push(` url: '${uriObj.protocol}//${uriObj.host}',`); | ||
if(allHeaders['content-type'] || allHeaders['Content-Type']) { | ||
push(` headers: {'Content-Type' => '${allHeaders['content-type'] || allHeaders['Content-Type']}'}`); | ||
} | ||
push(`)`); | ||
|
||
blank(); | ||
push(`response = conn.${method.toLowerCase()}('${uriObj.pathname}') do |req|`); | ||
|
||
const headers = Object.keys(allHeaders); | ||
if (headers.length) { | ||
headers.forEach(key => { | ||
if(key.toLowerCase() !== 'content-type') { | ||
push(` req.headers['${key}'] = '${escapeForSingleQuotes(allHeaders[key])}'`); | ||
} | ||
}); | ||
} | ||
|
||
Object.keys(queryObj).forEach(name => { | ||
const value = queryObj[name]; | ||
if (Array.isArray(value)) { | ||
push(` req.params['${name}'] = ${JSON.stringify(value)}`) | ||
} else { | ||
push(` req.params['${name}'] = '${value}'`) | ||
} | ||
}); | ||
|
||
switch (postData.mimeType) { | ||
case 'application/x-www-form-urlencoded': | ||
if (postData.params) { | ||
push(` req.body = URI.encode_www_form(data)`); | ||
} | ||
break; | ||
|
||
case 'application/json': | ||
if (postData.jsonObj) { | ||
push(` req.body = ${JSON.stringify(postData.text)}`); | ||
} | ||
break; | ||
|
||
default: | ||
if (postData.text) { | ||
push(` req.body = ${JSON.stringify(postData.text)}`); | ||
} | ||
} | ||
|
||
push('end'); | ||
blank() | ||
push('puts response.status'); | ||
push('puts response.body'); | ||
|
||
return join(); | ||
}, | ||
}; |
18 changes: 18 additions & 0 deletions
18
src/targets/ruby/faraday/fixtures/application-form-encoded.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'faraday' | ||
|
||
data = { | ||
:foo => "bar", | ||
:hello => "world", | ||
} | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'application/x-www-form-urlencoded'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.body = URI.encode_www_form(data) | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'application/json'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.body = "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}" | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.headers['cookie'] = 'foo=bar; bar=baz' | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Faraday cannot currently run PROPFIND requests. Please use another client. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'faraday' | ||
|
||
data = { | ||
:foo => "bar", | ||
} | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'application/x-www-form-urlencoded'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.headers['cookie'] = 'foo=bar; bar=baz' | ||
req.headers['accept'] = 'application/json' | ||
req.params['foo'] = ["bar","baz"] | ||
req.params['baz'] = 'abc' | ||
req.params['key'] = 'value' | ||
req.body = URI.encode_www_form(data) | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
) | ||
|
||
response = conn.get('/har') do |req| | ||
req.headers['accept'] = 'application/json' | ||
req.headers['x-foo'] = 'Bar' | ||
req.headers['quoted-value'] = '"quoted" \'string\'' | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'https://mockbin.com', | ||
) | ||
|
||
response = conn.get('/har') do |req| | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'application/json'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.body = "{\n \"foo\": \"bar\"\n}" | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'application/json'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.body = "{\"foo\":null}" | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'multipart/form-data; boundary=---011000010111000001101001'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"bar\"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--\r\n" | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'multipart/form-data; boundary=---011000010111000001101001'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n" | ||
end | ||
|
||
puts response.status | ||
puts response.body |
12 changes: 12 additions & 0 deletions
12
src/targets/ruby/faraday/fixtures/multipart-form-data-no-params.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'multipart/form-data'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'multipart/form-data; boundary=---011000010111000001101001'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n" | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
) | ||
|
||
response = conn.get('/har') do |req| | ||
req.params['foo[bar]'] = 'baz,zap' | ||
req.params['fiz'] = 'buz' | ||
req.params['key'] = 'value' | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
) | ||
|
||
response = conn.get('/har') do |req| | ||
req.params['foo'] = ["bar","baz"] | ||
req.params['baz'] = 'abc' | ||
req.params['key'] = 'value' | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
) | ||
|
||
response = conn.get('/har') do |req| | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'faraday' | ||
|
||
conn = Faraday.new( | ||
url: 'http://mockbin.com', | ||
headers: {'Content-Type' => 'text/plain'} | ||
) | ||
|
||
response = conn.post('/har') do |req| | ||
req.body = "Hello World" | ||
end | ||
|
||
puts response.status | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters