2013-10-04 36 views
-1

我有一個函數來測試我的URL看起來像這樣:測試網址 - 網址不存在返回200

def test_URLs(self): 

    routes = [ 
    'about/', 
    'archive/', 
    'index/', 
    'admin/', 
    '' 
    'doesntExist/' 
    ] 

    for route in routes: 
     response = self.client.get(route) 
     self.assertEqual(response.status_code, 200) 

和我的網址模式,看起來像這樣:

urlpatterns = patterns('', 
    #CMS url 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^$', 'core.views.index'), 
    url(r'^index/', 'core.views.index'), 
    url(r'about/', 'core.views.about'), 
    url(r'^archive/', 'core.views.archive'), 
    url(r'^talks/(?P<slug>[\w\-]+)/$', 'core.views.getTalk'), 

在我的test_URLs函數中,路由'doesntExist /'不存在,而非恰當。當我運行我的服務器,並嘗試訪問doesntExist/我得到的日誌消息

[04/Oct/2013 09:37:40] "GET /doesntExist/ HTTP/1.1" 404 2629

所以doesntExist/肯定還沒有,當我運行上面的測試中,我得到有:

Creating test database for alias 'default'... 
.. 
---------------------------------------------------------------------- 
Ran 2 tests in 0.017s 

OK 

爲什麼我的測試認爲它存在?

+0

這真的很大程度上取決於'self.client.get'中的代碼,但是URL'doesntExist /',前面沒有任何主機名,可以想見你的ISP會試圖幫助你「。它的DNS服務器可能會將不存在的主機名'doesntExist'解析到其自己的搜索頁面。 –

+0

有趣。我嘗試過'end_point ='http://127.0.0.1:8000/'+ route'並得到了迴應,但我仍然遇到同樣的問題。我怎麼能解決這個問題? – Nanor

+0

print'self.client.get('/ doesntExist /')。content'打印了什麼? –

回答

0

我的問題是我正在使用名爲Lockdown的應用程序。狀態碼返回爲200,因爲它獲取登錄HTML表單,無論用戶以前沒有登錄過什麼URL,它都會顯示。

只要在測試時不要使用Lockdown。