2015-12-26 42 views
0

我試圖用瓶客戶端測試瓶應用程序。瓶測試 - 功能測試錯誤 - 響應流[400不良請求]

我的應用途徑處理如下

@app.route('/', methods=['post', 'get']) 
@app.route('/index', methods=['post', 'get']) 
def index(): 

    login_form = LoginForm(request.form) 
    logout_form = LogoutForm(request.form) 
    short_url_form = ShortURLForm(request.form) 


    if request.method == 'POST' and short_url_form.validate(): 
     url = short_url_form.url.data 
     url_shortener_handler = urlShortener() 

     app.logger.debug('in post method to shorten Url(%s)', url) 

     # TODO have a mechanism for handling duplicate key error 
     short_url = url_shortener_handler.generateShortUrl() 

     if url_shortener_handler.saveUrl(short_url, url): 
      app.logger.debug('value of short url(%s) for url is (%s)', short_url, url) 
      return render_template('index.html', 
           login_form=login_form, 
           logout_form=logout_form, 
           shorturl_form=short_url_form, 
           shortURL=SITE_URL + '/' + short_url) 
     else: 
      app.logger.critical('Error in saving short url(%s) for url is (%s)', short_url, url) 
      flash('Internal error try again') 

    return render_template('index.html', 
         login_form=login_form, 
         logout_form=logout_form, 
         shorturl_form=short_url_form, 
         shortURL=None) 

而且short_url_form定義如下

class ShortURLForm(Form): 

    url = StringField('url', validators=[url(), data_required()]) 
    submit = SubmitField('Shorten') 

    def __init__(self, *args, **kwargs): 
     Form.__init__(self, *args, **kwargs) 

    def validate(self): 
     """ performs validation of input """ 
     if not Form.validate(self): 
      return False 
     return True 

,我與測試情況如下

class TestBasicUrlShortener(unittest.TestCase): 
    def setUp(self): 
     self.client = app.test_client() 
     self.baseURL = 'http://localhost:5000' 

    def create_app(self): 
     """ this is one of the functions that must be implemented for flask testing. """ 
     app = Flask(__name__) 
     app.config['TESTING'] = True 
     app.config['WTF_CSRF_ENABLED'] = False 
     WTF_CSRF_ENABLED = False 
     app.debug = True 
     self.baseURL = 'http://localhost:5000' 
     return app 

和測試當我使用客戶端發送郵件請求時,我收到400個錯誤的請求。

 def test_post_to_urlshortener(self): 
     """ When we send a post we expect it to return a output 
     containing the baseURL and short url """ 

     # monkeypatch the generate shortURL so that we know 
     # the correct value to expect and perform validation 
     # accordingly 
     from app.models import urlshortener 

      urlshortener.urlShortener.generateShortUrl = self.generate_shortURL 
     data = dict(url='http://www.google.com/', submit='Shorten') 

      rv = self.client.post('/', 
          data=data, 
          follow_redirects=False) 
      print rv 
      self.assertEqual(rv.status_code, 200) 
      shorturl = self.baseURL + '/' + self.generate_shortURL() 
      # print rv.data 
      assert shorturl in str(rv.data) 

      # cleanup so next time it works 
      urlshort = urlshortener.urlShortener() 
      urlshort.removeUrl(self.generate_shortURL()) 

代碼工作時,我用瀏覽器進行測試,即從測試失蹤,瀏覽器CSRF token.However我可以使用配置禁用CSRF保護的唯一參數(希望如此)

歡迎任何幫助縮小問題的指針。

+0

我沒有看到你調用'create_app'。你確定你不使用你的全球應用程序嗎? – ThiefMaster

+0

它是有道理的。你能否給出一個在測試客戶端中使用它的小例子來覆蓋全局應用程序 – Pradheep

回答

1

的問題似乎是,即使在csrf_token被禁用,WTF-形式的問題https://github.com/lepture/flask-wtf/issues/208

的工作圍繞提到的是引發錯誤我所做的是爲了使代碼,解析輸出和提取物csrf_token。代碼如下

def getcsrf_value(self): 

    """ get csrf token """ 

    rv = self.client.get('/') 

    soup = BeautifulSoup(rv.data, 'html.parser') 
    tag = soup.body.find('input', attrs={'name' : 'csrf_token'}) 
    return tag['value']