From 0d518513e0f79c5a3adffe63653fc67b1abe270e Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Thu, 25 May 2023 15:32:55 -0700 Subject: [PATCH] fix(clojure): don't crash if there's no postData content --- src/targets/clojure/clj_http/client.test.ts | 34 +++++++++++++++++++ src/targets/clojure/clj_http/client.ts | 4 ++- .../clj_http/fixtures/should-not-crash.clj | 4 +++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/targets/clojure/clj_http/client.test.ts create mode 100644 src/targets/clojure/clj_http/fixtures/should-not-crash.clj diff --git a/src/targets/clojure/clj_http/client.test.ts b/src/targets/clojure/clj_http/client.test.ts new file mode 100644 index 000000000..edb876683 --- /dev/null +++ b/src/targets/clojure/clj_http/client.test.ts @@ -0,0 +1,34 @@ +import type { Request } from '../../..'; + +import { runCustomFixtures } from '../../../fixtures/runCustomFixtures'; + +runCustomFixtures({ + targetId: 'clojure', + clientId: 'clj_http', + tests: [ + { + it: 'should not crash if there is no `postData.text`', + input: { + headers: [ + { + name: 'accept', + value: 'application/json', + }, + { + name: 'content-type', + value: 'application/json', + }, + ], + postData: { + mimeType: 'application/json', + }, + bodySize: 0, + method: 'POST', + url: 'https://httpbin.org/anything', + httpVersion: 'HTTP/1.1', + } as Request, + options: {}, + expected: 'should-not-crash.clj', + }, + ], +}); diff --git a/src/targets/clojure/clj_http/client.ts b/src/targets/clojure/clj_http/client.ts index 43f6449c6..06c20b7bc 100644 --- a/src/targets/clojure/clj_http/client.ts +++ b/src/targets/clojure/clj_http/client.ts @@ -45,7 +45,9 @@ const jsType = (input?: any) => { }; const objEmpty = (input?: any) => { - if (jsType(input) === 'object') { + if (input === undefined) { + return true; + } else if (jsType(input) === 'object') { return Object.keys(input).length === 0; } return false; diff --git a/src/targets/clojure/clj_http/fixtures/should-not-crash.clj b/src/targets/clojure/clj_http/fixtures/should-not-crash.clj new file mode 100644 index 000000000..382de39e1 --- /dev/null +++ b/src/targets/clojure/clj_http/fixtures/should-not-crash.clj @@ -0,0 +1,4 @@ +(require '[clj-http.client :as client]) + +(client/post "https://httpbin.org/anything" {:content-type :json + :accept :json}) \ No newline at end of file