|
10 | 10 | [clojure.string :as string]
|
11 | 11 | [babashka.json :as json]
|
12 | 12 | [babashka.http-client :as http-client])
|
13 |
| - (:import [java.net URI URL])) |
| 13 | + (:import [java.net URI URL] |
| 14 | + [java.io IOException])) |
14 | 15 |
|
15 | 16 | (defn fixup-request
|
16 | 17 | "Convert spider request to format expected by validator"
|
|
82 | 83 | (assoc :uri (select-keys req [:host :port :path :scheme]))
|
83 | 84 | (dissoc :host :port :path :scheme)))
|
84 | 85 |
|
85 |
| -(defn fixup-validator-request |
86 |
| - [req] |
87 |
| - (assoc req :uri (:path req))) |
| 86 | +(defn wrap-io-exception-handler [f] |
| 87 | + (fn [request] |
| 88 | + (try |
| 89 | + (f request) |
| 90 | + (catch IOException ex |
| 91 | + {:status 0, :body (.getMessage ex), :headers {}})))) |
88 | 92 |
|
89 | 93 | (defn wrap-client
|
90 | 94 | [f client]
|
|
195 | 199 | (defn mk-exec-request
|
196 | 200 | [{:keys [base-url headers basic-auth bearer-token]}]
|
197 | 201 | (cond-> http-client/request
|
| 202 | + :always |
| 203 | + (wrap-io-exception-handler) |
| 204 | + |
198 | 205 | :always
|
199 | 206 | (wrap-client (http-client/client (assoc http-client/default-client-opts
|
200 | 207 | :follow-redirects :never)))
|
|
226 | 233 | 'assoc assoc
|
227 | 234 | 'dissoc dissoc))
|
228 | 235 |
|
| 236 | +(defn wrap-network-error [validate] |
| 237 | + (fn [{:keys [response] :as interaction} path] |
| 238 | + (if (= 0 (:status response)) |
| 239 | + [{:issue "network-error" |
| 240 | + :hints {:message (:body response)}}] |
| 241 | + (validate interaction path)))) |
| 242 | + |
229 | 243 | (defn spider-and-validate
|
230 | 244 | [openapi-spec
|
231 | 245 | {:keys [rules seeds] :as _profile}
|
232 | 246 | {:keys [max-requests-per-operation max-total-requests spider-timeout-millis] :as options}]
|
233 | 247 | (let [seeds (or (:seeds options) seeds)
|
234 | 248 | validate (-> (validator/validator-context openapi-spec {})
|
235 |
| - (validator/interaction-validator)) |
| 249 | + (validator/interaction-validator) |
| 250 | + (wrap-network-error)) |
236 | 251 | matcher (paths-matcher (keys (get openapi-spec "paths")))
|
237 | 252 | op-path (fn [request]
|
238 | 253 | (when-let [template (:template (matcher (path request)))]
|
|
0 commit comments