Skip to content

Commit

Permalink
feat: setting up request messages
Browse files Browse the repository at this point in the history
adds the possibility to set request headers and body

fix #32 fix #42
  • Loading branch information
tpluscode committed Jul 26, 2019
1 parent c236028 commit 4ff1abb
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ LinkStatement:
;

HeaderStatement:
ExpectModifier 'Header' fieldName=(ID | HYPHENATED_NAME) (exactValue=STRING | regex=MatchingRegex | variable=VARIABLE)?
ExpectModifier 'Header' fieldName=FieldName (exactValue=STRING | regex=MatchingRegex | variable=VARIABLE)?
;

MatchingRegex:
Expand All @@ -81,5 +81,37 @@ FollowStatement:
'Follow' variable=VARIABLE
;

RequestBlock:
{RequestBlock} '{'
(headers+=RequestHeader)*
(body=RequestBody)?
'}'
;

ResponseBlock:
{ResponseBlock} '{'
(children+=ResponseStep)*
'}'
;

RequestBody:
contents=MULTILINE_BLOCK |
reference=RequestFileBody
;

RequestFileBody:
'<<<' path=STRING
;

RequestHeader:
fieldName=FieldName value=STRING
;

FieldName: (ID | HYPHENATED_NAME);

terminal VARIABLE: '[' ID ']';
terminal HYPHENATED_NAME: ('a'..'z' | 'A'..'Z') ('-' | 'a'..'z' | 'A'..'Z')*;

terminal MULTILINE_BLOCK:
'```' -> '```'
;
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ OperationBlock:
;

InvocationBlock:
{InvocationBlock} 'Invoke' '{'
(children+=ResponseStep)*
'}'
'Invoke' (request=RequestBlock '=>')? response=ResponseBlock
;

terminal URI:
'<' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'>') )* '>'
'<' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'>'|'<') )* '>'
;
terminal PREFIX: ID ':';
terminal QNAME: PREFIX (INT | ID | '/' | '%')+;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class CoreValueConverterService extends DefaultTerminalConverters {
return new VariableReferenceConverter()
}

@ValueConverter(rule = "MULTILINE_BLOCK")
def IValueConverter<String> getMultilineBlockConverter() {
return new MultilineBlockConverter()
}

@ValueConverter(rule = "PREFIX")
def IValueConverter<String> getPrefixConverter() {
return new PrefixConverter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app.hypermedia.testing.dsl.conversion

import org.eclipse.xtext.conversion.IValueConverter
import org.eclipse.xtext.conversion.ValueConverterException
import org.eclipse.xtext.nodemodel.INode

class MultilineBlockConverter implements IValueConverter<String> {
final String INVALID_VALUE_ERROR = "Code block must be between triple backticks"

override toString(String value) {
return value
}

override toValue(String string, INode node) throws ValueConverterException {
if (string === null) {
return null
}

if(string.length < 7 || !string.startsWith("```") || !string.endsWith("```")) {
throw new ValueConverterException(INVALID_VALUE_ERROR, node, null)
}

return string.substring(3, string.length - 3).trim()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import app.hypermedia.testing.dsl.hydra.OperationBlock
import app.hypermedia.testing.dsl.hydra.InvocationBlock
import app.hypermedia.testing.dsl.Modifier
import java.util.HashMap
import org.json.JSONObject
import app.hypermedia.testing.dsl.hydra.UriName
import app.hypermedia.testing.dsl.hydra.PrefixedName
import org.eclipse.xtext.generator.IFileSystemAccess2
Expand Down Expand Up @@ -51,7 +52,28 @@ final Map<String, String> _namespaces
def dispatch step(InvocationBlock it) {
val map = new HashMap<String, Object>

return buildBlock('Invocation', children, map)
if(request !== null) {
if(request.headers.length > 0) {
val headers = new JSONObject
request.headers.forEach[header |
headers.put(header.fieldName, header.value)
]

map.put('headers', headers)
}

if(request.body !== null) {
if (request.body.reference !== null) {
val body = new JSONObject()
body.put('path', request.body.reference.path)
map.put('body', body)
} else {
map.put('body', request.body.contents)
}
}
}

return buildBlock('Invocation', response.children, map)
}

def dispatch identifier(UriName it) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
app.hypermedia.testing.dsl.tests.generator.hydra.InvokeTest.classOperation_invokeTwiceWithRequests_generatesSteps=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"body": {
"map": {
"path": "/some/path/data.csv"
},
"empty": false
},
"children": {
"myArrayList": [
{
"map": {
"code": 201,
"type": "ResponseStatus"
},
"empty": false
}
],
"empty": false
},
"headers": {
"map": {
"Content-Type": "text/csv"
},
"empty": false
},
"type": "Invocation"
},
"empty": false
},
{
"map": {
"body": "{\n \"hello\": \"world\",\n \"foo\": \"bar\"\n }",
"children": {
"myArrayList": [
{
"map": {
"code": 409,
"type": "ResponseStatus"
},
"empty": false
}
],
"empty": false
},
"headers": {
"map": {
"Content-Type": "application/json"
},
"empty": false
},
"type": "Invocation"
},
"empty": false
}
],
"empty": false
},
"operationId": "http://example.com/CreateUser",
"strict": false,
"type": "Operation"
},
"empty": false
}
],
"empty": false
},
"classId": "http://example.com/People",
"type": "Class"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]


app.hypermedia.testing.dsl.tests.generator.hydra.InvokeTest.topLevelOperation_invokeTwiceWithRequests_generatesSteps=[
{
"map": {
"steps": {
"myArrayList": [
{
"map": {
"children": {
"myArrayList": [
{
"map": {
"body": {
"map": {
"path": "/some/path/data.csv"
},
"empty": false
},
"children": {
"myArrayList": [
{
"map": {
"code": 201,
"type": "ResponseStatus"
},
"empty": false
}
],
"empty": false
},
"headers": {
"map": {
"Content-Type": "text/csv"
},
"empty": false
},
"type": "Invocation"
},
"empty": false
},
{
"map": {
"body": "{\n \"hello\": \"world\",\n \"foo\": \"bar\"\n }",
"children": {
"myArrayList": [
{
"map": {
"code": 409,
"type": "ResponseStatus"
},
"empty": false
}
],
"empty": false
},
"headers": {
"map": {
"Content-Type": "application/json"
},
"empty": false
},
"type": "Invocation"
},
"empty": false
}
],
"empty": false
},
"operationId": "http://example.com/CreateUser",
"strict": false,
"type": "Operation"
},
"empty": false
}
],
"empty": false
}
},
"empty": false
}
]

0 comments on commit 4ff1abb

Please sign in to comment.