|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, FusionAuth, All Rights Reserved |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an |
| 12 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 13 | + * either express or implied. See the License for the specific |
| 14 | + * language governing permissions and limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +import * as chai from 'chai'; |
| 20 | +import DefaultRESTClient from "../src/DefaultRESTClient" |
| 21 | +import {URLSearchParams} from "url"; |
| 22 | + |
| 23 | + |
| 24 | +describe('#DefaultRESTClient()', function () { |
| 25 | + |
| 26 | + it('Can Create DefaultRESTClient', async () => { |
| 27 | + const client = new DefaultRESTClient('http://localhost:9011'); |
| 28 | + chai.assert.isNotNull(client); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('withFormData', function () { |
| 32 | + it('null', async () => { |
| 33 | + const client = new DefaultRESTClient('http://localhost:9011'); |
| 34 | + let body = client.withFormData(null).body; |
| 35 | + chai.assert.isNull(body); |
| 36 | + }); |
| 37 | + |
| 38 | + it('empty', async () => { |
| 39 | + const client = new DefaultRESTClient('http://localhost:9011'); |
| 40 | + let params = new URLSearchParams(); |
| 41 | + let body = client.withFormData(params).body; |
| 42 | + chai.assert.isNotNull(body); |
| 43 | + chai.assert.strictEqual(body.toString(), ""); |
| 44 | + }); |
| 45 | + |
| 46 | + it('with one value', async () => { |
| 47 | + const client = new DefaultRESTClient('http://localhost:9011'); |
| 48 | + let params = new URLSearchParams(); |
| 49 | + params.set('key','value'); |
| 50 | + let body = client.withFormData(params).body; |
| 51 | + chai.assert.isNotNull(body); |
| 52 | + chai.assert.strictEqual(body.toString(), "key=value"); |
| 53 | + }); |
| 54 | + |
| 55 | + it('with two values', async () => { |
| 56 | + const client = new DefaultRESTClient('http://localhost:9011'); |
| 57 | + let params = new URLSearchParams(); |
| 58 | + params.set('key','value'); |
| 59 | + params.set('key2','value2'); |
| 60 | + let body = client.withFormData(params).body; |
| 61 | + chai.assert.isNotNull(body); |
| 62 | + chai.assert.strictEqual(body.toString(), "key=value&key2=value2"); |
| 63 | + }); |
| 64 | + |
| 65 | + it('skips undefined value', async () => { |
| 66 | + const client = new DefaultRESTClient('http://localhost:9011'); |
| 67 | + let params = new URLSearchParams(); |
| 68 | + params.set('key','value'); |
| 69 | + params.set('key2',undefined); |
| 70 | + let body = client.withFormData(params).body; |
| 71 | + chai.assert.isNotNull(body); |
| 72 | + chai.assert.strictEqual(body.toString(), "key=value"); |
| 73 | + }); |
| 74 | + |
| 75 | + it('skips null value', async () => { |
| 76 | + const client = new DefaultRESTClient('http://localhost:9011'); |
| 77 | + let params = new URLSearchParams(); |
| 78 | + params.set('key','value'); |
| 79 | + params.set('key2',null); |
| 80 | + let body = client.withFormData(params).body; |
| 81 | + chai.assert.isNotNull(body); |
| 82 | + chai.assert.strictEqual(body.toString(), "key=value"); |
| 83 | + }); |
| 84 | + |
| 85 | + it('sets content type', async () => { |
| 86 | + const client = new DefaultRESTClient('http://localhost:9011'); |
| 87 | + let params = new URLSearchParams(); |
| 88 | + let headers = client.withFormData(params).headers |
| 89 | + chai.assert.isNotNull(headers); |
| 90 | + chai.assert.strictEqual(headers['Content-Type'], "application/x-www-form-urlencoded"); |
| 91 | + }); |
| 92 | + |
| 93 | + }); |
| 94 | + |
| 95 | +}); |
0 commit comments