|
| 1 | +package au.com.dius.pact.consumer.dsl |
| 2 | + |
| 3 | +import au.com.dius.pact.consumer.xml.PactXmlBuilder |
| 4 | +import au.com.dius.pact.core.model.generators.Category |
| 5 | +import au.com.dius.pact.core.model.generators.ProviderStateGenerator |
| 6 | +import au.com.dius.pact.core.model.matchingrules.ContentTypeMatcher |
| 7 | +import au.com.dius.pact.core.model.matchingrules.MatchingRuleCategory |
| 8 | +import au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup |
| 9 | +import au.com.dius.pact.core.model.matchingrules.RegexMatcher |
| 10 | +import au.com.dius.pact.core.model.v4.MessageContents |
| 11 | +import spock.lang.Specification |
| 12 | + |
| 13 | +import static au.com.dius.pact.consumer.dsl.Matchers.fromProviderState |
| 14 | +import static au.com.dius.pact.consumer.dsl.Matchers.regexp |
| 15 | + |
| 16 | +class MessageContentsBuilderSpec extends Specification { |
| 17 | + |
| 18 | + MessageContentsBuilder builder |
| 19 | + |
| 20 | + def setup() { |
| 21 | + builder = new MessageContentsBuilder(new MessageContents()) |
| 22 | + } |
| 23 | + |
| 24 | + def 'allows adding metadata to the message'() { |
| 25 | + when: |
| 26 | + def message = builder |
| 27 | + .withMetadata([x: 'y', y: ['a', 'b', 'c']]) |
| 28 | + .build() |
| 29 | + |
| 30 | + then: |
| 31 | + message.metadata == [ |
| 32 | + 'x': 'y', |
| 33 | + 'y': ['a', 'b', 'c'] |
| 34 | + ] |
| 35 | + } |
| 36 | + |
| 37 | + def 'allows using matching rules with the metadata'() { |
| 38 | + when: |
| 39 | + def message = builder |
| 40 | + .withMetadata([x: regexp('\\d+', '111')]) |
| 41 | + .build() |
| 42 | + |
| 43 | + then: |
| 44 | + message.metadata == [ |
| 45 | + 'x': '111' |
| 46 | + ] |
| 47 | + message.matchingRules.rulesForCategory('metadata') == new MatchingRuleCategory('metadata', |
| 48 | + [ |
| 49 | + x: new MatchingRuleGroup([new RegexMatcher('\\d+', '111')]) |
| 50 | + ] |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + def 'supports setting metadata values from provider states'() { |
| 55 | + when: |
| 56 | + def message = builder |
| 57 | + .withMetadata(['A': fromProviderState('$a', '111')]) |
| 58 | + .build() |
| 59 | + |
| 60 | + then: |
| 61 | + message.metadata == [ |
| 62 | + 'A': '111' |
| 63 | + ] |
| 64 | + message.matchingRules.rulesForCategory('metadata') == new MatchingRuleCategory('metadata', [:]) |
| 65 | + message.generators.categoryFor(Category.METADATA) == [A: new ProviderStateGenerator('$a')] |
| 66 | + } |
| 67 | + |
| 68 | + def 'allows setting the contents of the message as a string value'() { |
| 69 | + when: |
| 70 | + def message = builder |
| 71 | + .withContent('This is some text') |
| 72 | + .build() |
| 73 | + |
| 74 | + then: |
| 75 | + message.contents.valueAsString() == 'This is some text' |
| 76 | + message.contents.contentType.toString() == 'text/plain; charset=ISO-8859-1' |
| 77 | + message.metadata['contentType'] == 'text/plain; charset=ISO-8859-1' |
| 78 | + } |
| 79 | + |
| 80 | + def 'allows setting the contents of the message as a string value with a given content type'() { |
| 81 | + when: |
| 82 | + def message = builder |
| 83 | + .withContent('This is some text', 'text/test-special') |
| 84 | + .build() |
| 85 | + |
| 86 | + then: |
| 87 | + message.contents.valueAsString() == 'This is some text' |
| 88 | + message.contents.contentType.toString() == 'text/test-special' |
| 89 | + message.metadata['contentType'] == 'text/test-special' |
| 90 | + } |
| 91 | + |
| 92 | + def 'when setting the body, tries to detect the content type from the body contents'() { |
| 93 | + when: |
| 94 | + def message = builder |
| 95 | + .withContent('{"value": "This is some text"}') |
| 96 | + .build() |
| 97 | + |
| 98 | + then: |
| 99 | + message.contents.valueAsString() == '{"value": "This is some text"}' |
| 100 | + message.contents.contentType.toString() == 'application/json' |
| 101 | + message.metadata['contentType'] == 'application/json' |
| 102 | + } |
| 103 | + |
| 104 | + def 'when setting the body, uses any existing content type metadata value'() { |
| 105 | + when: |
| 106 | + def message = builder |
| 107 | + .withMetadata(['contentType': 'text/plain']) |
| 108 | + .withContent('{"value": "This is some text"}') |
| 109 | + .build() |
| 110 | + |
| 111 | + then: |
| 112 | + message.contents.valueAsString() == '{"value": "This is some text"}' |
| 113 | + message.contents.contentType.toString() == 'text/plain' |
| 114 | + message.metadata['contentType'] == 'text/plain' |
| 115 | + } |
| 116 | + |
| 117 | + def 'when setting the body, overrides any existing content type header if the content type is given'() { |
| 118 | + when: |
| 119 | + def message = builder |
| 120 | + .withMetadata(['contentType': 'text/plain']) |
| 121 | + .withContent('{"value": "This is some text"}', 'application/json') |
| 122 | + .build() |
| 123 | + |
| 124 | + then: |
| 125 | + message.contents.valueAsString() == '{"value": "This is some text"}' |
| 126 | + message.contents.contentType.toString() == 'application/json' |
| 127 | + message.metadata['contentType'] == 'application/json' |
| 128 | + } |
| 129 | + |
| 130 | + def 'supports setting the body from a DSLPart object'() { |
| 131 | + when: |
| 132 | + def message = builder |
| 133 | + .withContent(new PactDslJsonBody().stringType('value', 'This is some text')) |
| 134 | + .build() |
| 135 | + |
| 136 | + then: |
| 137 | + message.contents.valueAsString() == '{"value":"This is some text"}' |
| 138 | + message.contents.contentType.toString() == 'application/json' |
| 139 | + message.metadata['contentType'] == 'application/json' |
| 140 | + message.matchingRules.rulesForCategory('body') == new MatchingRuleCategory('body', |
| 141 | + [ |
| 142 | + '$.value': new MatchingRuleGroup([au.com.dius.pact.core.model.matchingrules.TypeMatcher.INSTANCE]) |
| 143 | + ] |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + def 'supports setting the body using a body builder'() { |
| 148 | + when: |
| 149 | + def message = builder |
| 150 | + .withContent(new PactXmlBuilder('test').build { |
| 151 | + it.attributes = [id: regexp('\\d+', '100')] |
| 152 | + }) |
| 153 | + .build() |
| 154 | + |
| 155 | + then: |
| 156 | + message.contents.valueAsString() == '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' + |
| 157 | + System.lineSeparator() + '<test id="100"/>' + System.lineSeparator() |
| 158 | + message.contents.contentType.toString() == 'application/xml' |
| 159 | + message.metadata['contentType'] == 'application/xml' |
| 160 | + message.matchingRules.rulesForCategory('body') == new MatchingRuleCategory('body', |
| 161 | + [ |
| 162 | + '$.test[\'@id\']': new MatchingRuleGroup([new RegexMatcher('\\d+', '100')]) |
| 163 | + ] |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + def 'supports setting up a content type matcher on the body'() { |
| 168 | + given: |
| 169 | + def gif1px = [ |
| 170 | + 0107, 0111, 0106, 0070, 0067, 0141, 0001, 0000, 0001, 0000, 0200, 0000, 0000, 0377, 0377, 0377, |
| 171 | + 0377, 0377, 0377, 0054, 0000, 0000, 0000, 0000, 0001, 0000, 0001, 0000, 0000, 0002, 0002, 0104, |
| 172 | + 0001, 0000, 0073 |
| 173 | + ] as byte[] |
| 174 | + |
| 175 | + when: |
| 176 | + def message = builder |
| 177 | + .withContentsMatchingContentType('image/gif', gif1px) |
| 178 | + .build() |
| 179 | + |
| 180 | + then: |
| 181 | + message.contents.value == gif1px |
| 182 | + message.contents.contentType.toString() == 'image/gif' |
| 183 | + message.metadata['contentType'] == 'image/gif' |
| 184 | + message.matchingRules.rulesForCategory('body') == new MatchingRuleCategory('body', |
| 185 | + [ |
| 186 | + '$': new MatchingRuleGroup([new ContentTypeMatcher('image/gif')]) |
| 187 | + ] |
| 188 | + ) |
| 189 | + } |
| 190 | + |
| 191 | + def 'allows setting the contents of the message as a byte array'() { |
| 192 | + given: |
| 193 | + def gif1px = [ |
| 194 | + 0107, 0111, 0106, 0070, 0067, 0141, 0001, 0000, 0001, 0000, 0200, 0000, 0000, 0377, 0377, 0377, |
| 195 | + 0377, 0377, 0377, 0054, 0000, 0000, 0000, 0000, 0001, 0000, 0001, 0000, 0000, 0002, 0002, 0104, |
| 196 | + 0001, 0000, 0073 |
| 197 | + ] as byte[] |
| 198 | + |
| 199 | + when: |
| 200 | + def message = builder |
| 201 | + .withContent(gif1px) |
| 202 | + .build() |
| 203 | + |
| 204 | + then: |
| 205 | + message.contents.unwrap() == gif1px |
| 206 | + message.contents.contentType.toString() == 'application/octet-stream' |
| 207 | + message.metadata['contentType'] == 'application/octet-stream' |
| 208 | + } |
| 209 | + |
| 210 | + def 'allows setting the contents of the message as a a byte array with a content type'() { |
| 211 | + given: |
| 212 | + def gif1px = [ |
| 213 | + 0107, 0111, 0106, 0070, 0067, 0141, 0001, 0000, 0001, 0000, 0200, 0000, 0000, 0377, 0377, 0377, |
| 214 | + 0377, 0377, 0377, 0054, 0000, 0000, 0000, 0000, 0001, 0000, 0001, 0000, 0000, 0002, 0002, 0104, |
| 215 | + 0001, 0000, 0073 |
| 216 | + ] as byte[] |
| 217 | + |
| 218 | + when: |
| 219 | + def message = builder |
| 220 | + .withContent(gif1px, 'image/gif') |
| 221 | + .build() |
| 222 | + |
| 223 | + then: |
| 224 | + message.contents.unwrap() == gif1px |
| 225 | + message.contents.contentType.toString() == 'image/gif' |
| 226 | + message.metadata['contentType'] == 'image/gif' |
| 227 | + } |
| 228 | +} |
0 commit comments