Skip to content

Commit

Permalink
fix(webhook): Allow content-type text/plain in webhook stage (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbackes authored and robzienert committed Aug 22, 2017
1 parent 9064951 commit c2332f1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

package com.netflix.spinnaker.orca.webhook.config

import com.netflix.spinnaker.orca.webhook.service.WebhookService
import ch.qos.logback.classic.pattern.MessageConverter
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.web.client.RestTemplate;
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.http.converter.StringHttpMessageConverter
import org.springframework.web.client.RestTemplate

@Configuration
@ConditionalOnProperty(prefix = "webhook.stage", value = "enabled", matchIfMissing = true)
Expand All @@ -35,7 +37,17 @@ class WebhookConfiguration {
@Bean
@ConditionalOnMissingBean(RestTemplate)
RestTemplate restTemplate() {
new RestTemplate()
RestTemplate restTemplate = new RestTemplate()
List<MessageConverter> converters = restTemplate.getMessageConverters()
converters.add(new ObjectStringHttpMessageConverter())
restTemplate.setMessageConverters(converters)
return restTemplate
}

class ObjectStringHttpMessageConverter extends StringHttpMessageConverter implements HttpMessageConverter<Object> {
@Override
boolean supports(Class<?> clazz) {
return Object.class.equals(clazz)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package com.netflix.spinnaker.orca.webhook.service

import com.netflix.spinnaker.orca.config.UserConfiguredUrlRestrictions
import com.netflix.spinnaker.orca.webhook.config.PreconfiguredWebhookProperties
import com.netflix.spinnaker.orca.webhook.config.WebhookConfiguration
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.test.web.client.MockRestServiceServer
import org.springframework.test.web.client.ResponseActions
import org.springframework.web.client.RestTemplate
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Subject
Expand All @@ -40,7 +40,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
class WebhookServiceSpec extends Specification {

@Shared
def restTemplate = new RestTemplate()
def restTemplate = new WebhookConfiguration().restTemplate();

@Shared
def userConfiguredUrlRestrictions = new UserConfiguredUrlRestrictions.Builder().withRejectLocalhost(false).build()
Expand Down Expand Up @@ -133,4 +133,26 @@ class WebhookServiceSpec extends Specification {
then:
preconfiguredWebhooks == [webhook1, webhook3]
}

def "Content-Type text/plain is turned into a string"() {
expect:
def responseActions = server.expect(requestTo("https://localhost/v1/text/test"))
.andExpect(method(HttpMethod.GET))
responseActions.andRespond(withSuccess('This is text/plain', MediaType.TEXT_PLAIN))

when:
webhookService
def responseEntity = webhookService.exchange(
HttpMethod.GET,
"https://localhost/v1/text/test",
null,
null
)

then:
server.verify()
responseEntity.statusCode == HttpStatus.OK
responseEntity.body == 'This is text/plain'
}

}

0 comments on commit c2332f1

Please sign in to comment.