首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试静态路由

测试静态路由
EN

Stack Overflow用户
提问于 2013-12-04 13:51:34
回答 1查看 1.2K关注 0票数 3

我正在尝试为组合静态内容路由编写一个测试。我是通过直接检查环响应来测试路线的。

一个最低限度的工作示例如下:

代码语言:javascript
复制
;; src/testing-webapps.core.clj
(ns testing-webapps.core
  (:use [compojure.core]
        [compojure.route   :as route]))

(defroutes web-app
   (route/resources "/")
   (route/not-found "404"))


;; test/testing-webapps.core_test.clj
(ns testing-webapps.core-test
  (:require [clojure.test :refer :all]
            [testing-webapps.core :refer :all]))

(defn request [resource web-app & params]
  (web-app {:request-method :get :uri resource :params (first params)}))

(deftest test-routes
  (is (= 404 (:status (request "/flubber" web-app))))
  (is (= "404" (:body (request "/flubber" web-app))))
  (is (= 200 (:status (request "/test.txt" web-app)))))

测试404路由运行良好,但是调用(request "/test.txt" web-app)会导致ring.middleware.file-info/not-modified-since?中的意外NullPointerException

下面是堆栈跟踪的顶部部分:

代码语言:javascript
复制
ERROR in (test-routes) (file_info.clj:27)
Uncaught exception, not in assertion.
expected: nil
  actual: java.lang.NullPointerException: null
 at ring.middleware.file_info$not_modified_since_QMARK_.invoke (file_info.clj:27)
    ring.middleware.file_info$file_info_response.doInvoke (file_info.clj:44)
    clojure.lang.RestFn.invoke (RestFn.java:442)
    ring.middleware.file_info$wrap_file_info$fn__917.invoke (file_info.clj:64)
    [...]

静态路由在浏览器中运行良好,但在通过我的request函数调用时却不能工作。

是否有更简单的方法来测试静态路由?为什么我在使用自己的请求映射调用静态路由时获得一个NullPointerException?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-04 15:00:21

查看not-modified-since?的源代码,我认为问题在于您的请求映射中没有标头,因此它在这个expr:(headers "if-modified-since")上抛出了一个NPE。尝试更改您的request方法如下:

代码语言:javascript
复制
(defn request [resource web-app & params]
  (web-app {:request-method :get 
            :headers {"content-type" "text/plain"} ; added a header
            :uri resource 
            :params (first params)}))

您还可以考虑使用ring-mock创建测试请求。它会让你有点远离这种东西。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20377132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档