1+ package com.baeldung.webservice
2+
3+ import groovy.json.JsonSlurper
4+ import wslite.rest.ContentType
5+ import wslite.rest.RESTClient
6+ import wslite.rest.RESTClientException
7+ import wslite.soap.SOAPClient
8+ import wslite.soap.SOAPMessageBuilder
9+
10+ class WebserviceUnitTest extends GroovyTestCase {
11+
12+ JsonSlurper jsonSlurper = new JsonSlurper ()
13+
14+ static RESTClient client = new RESTClient (" https://postman-echo.com" )
15+
16+ static {
17+ client. defaultAcceptHeader = ContentType . JSON
18+ client. httpClient. sslTrustAllCerts = true
19+ }
20+
21+ void testHttpGetRequest () {
22+ def postmanGet = new URL (' https://postman-echo.com/get' )
23+ def getConnection = postmanGet. openConnection()
24+ getConnection. requestMethod = ' GET'
25+ assert getConnection. responseCode == 200
26+ if (getConnection. responseCode == 200 ) {
27+ assert jsonSlurper. parseText(getConnection. content. text)?. headers?. host == " postman-echo.com"
28+ }
29+ }
30+
31+ void testHttpPostRequest () {
32+ def postmanPost = new URL (' https://postman-echo.com/post' )
33+ def query = " q=This is post request form parameter."
34+ def postConnection = postmanPost. openConnection()
35+ postConnection. requestMethod = ' POST'
36+ assert postConnection. responseCode == 200
37+ }
38+
39+ void testHttpPostRequestWithParams () {
40+ def postmanPost = new URL (' https://postman-echo.com/post' )
41+ def form = " param1=This is request parameter."
42+ def postConnection = postmanPost. openConnection()
43+ postConnection. requestMethod = ' POST'
44+ postConnection. doOutput = true
45+ def text
46+ postConnection. with {
47+ outputStream. withWriter { outputStreamWriter ->
48+ outputStreamWriter << form
49+ }
50+ text = content. text
51+ }
52+ assert postConnection. responseCode == 200
53+ assert jsonSlurper. parseText(text)?. json. param1 == " This is request parameter."
54+ }
55+
56+ void testRssFeed () {
57+ def rssFeed = new XmlParser (). parse(" https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en" )
58+ def stories = []
59+ (0 .. 4 ). each {
60+ def item = rssFeed. channel. item. get(it)
61+ stories << item. title. text()
62+ }
63+ assert stories. size() == 5
64+ }
65+
66+ void testAtomFeed () {
67+ def atomFeed = new XmlParser (). parse(" https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en" )
68+ def stories = []
69+ (0 .. 4 ). each {
70+ def entry = atomFeed. entry. get(it)
71+ stories << entry. title. text()
72+ }
73+ assert stories. size() == 5
74+ }
75+
76+ void testSoapClient () {
77+ def url = " http://www.dataaccess.com/webservicesserver/numberconversion.wso"
78+ def soapClient = new SOAPClient (url)
79+ def message = new SOAPMessageBuilder (). build({
80+ body {
81+ NumberToWords (xmlns : " http://www.dataaccess.com/webservicesserver/" ) {
82+ ubiNum(1234 )
83+ }
84+ }
85+ })
86+ def response = soapClient. send(message. toString());
87+ def words = response.NumberToWordsResponse
88+ assert words == " one thousand two hundred and thirty four "
89+ }
90+
91+ void testRestClientGet () {
92+ def path = " /get"
93+ def response
94+ try {
95+ response = client. get(path : path)
96+ assert response. statusCode == 200
97+ assert response. json?. headers?. host == " postman-echo.com"
98+ } catch (RESTClientException e) {
99+ assert e?. response?. statusCode != 200
100+ }
101+ }
102+
103+ void testRestClientPost () {
104+ def path = " /post"
105+ def params = [" foo" :1 ," bar" :2 ]
106+ def response
107+ try {
108+ response = client. post(path : path) {
109+ type ContentType . JSON
110+ json params
111+ }
112+ assert response. json?. data == params
113+ } catch (RESTClientException e) {
114+ e. printStackTrace()
115+ assert e?. response?. statusCode != 200
116+ }
117+ }
118+
119+ void testBasicAuthentication () {
120+ def path = " /basic-auth"
121+ def response
122+ try {
123+ response = client. get(path : path, headers : [" Authorization" : " Basic cG9zdG1hbjpwYXNzd29yZA==" ])
124+ assert response. statusCode == 200
125+ assert response. json?. authenticated == true
126+ } catch (RESTClientException e) {
127+ assert e?. response?. statusCode != 200
128+ }
129+ }
130+
131+ void testOAuth () {
132+ RESTClient oAuthClient = new RESTClient (" https://postman-echo.com" )
133+ oAuthClient. defaultAcceptHeader = ContentType . JSON
134+ oAuthClient. httpClient. sslTrustAllCerts = true
135+
136+ def path = " /oauth1"
137+ def params = [oauth_consumer_key : " RKCGzna7bv9YD57c" , oauth_signature_method : " HMAC-SHA1" , oauth_timestamp :1567089944 , oauth_nonce : " URT7v4" , oauth_version : 1.0 , oauth_signature : ' RGgR/ktDmclkM0ISWaFzebtlO0A=' ]
138+ def response
139+ try {
140+ response = oAuthClient. get(path : path, query : params)
141+ assert response. statusCode == 200
142+ assert response. statusMessage == " OK"
143+ assert response. json. status == " pass"
144+ } catch (RESTClientException e) {
145+ assert e?. response?. statusCode != 200
146+ }
147+ }
148+ }
0 commit comments