Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split test / add error html #91

Merged
merged 2 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions test/file/search-gaokao-gzh-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="//www.sogou.com/images/logo2014/new/favicon.ico" type="image/x-icon">
<title>搜狗搜索</title>
<link rel="stylesheet" href="static/css/anti.min.css?v=1"/>
<script src="//dl.web.sogoucdn.com/common/lib/jquery/jquery-1.11.0.min.js"></script>
<script src="static/js/antispider.min.js?v=2"></script>
<script>
var domain = getDomain();
window.imgCode = -1;

(function() {
function checkSNUID() {
var cookieArr = document.cookie.split('; '),
count = 0;

for(var i = 0, len = cookieArr.length; i < len; i++) {
if (cookieArr[i].indexOf('SNUID=') > -1) {
count++;
}
}

return count > 1;
}

if(checkSNUID()) {
var date = new Date(), expires;
date.setTime(date.getTime() -100000);

expires = date.toGMTString();

document.cookie = 'SNUID=1;path=/;expires=' + expires;
document.cookie = 'SNUID=1;path=/;expires=' + expires + ';domain=.www.sogou.com';
document.cookie = 'SNUID=1;path=/;expires=' + expires + ';domain=.weixin.sogou.com';
document.cookie = 'SNUID=1;path=/;expires=' + expires + ';domain=.sogou.com';
document.cookie = 'SNUID=1;path=/;expires=' + expires + ';domain=.snapshot.sogoucdn.com';

sendLog('delSNUID');
}

if(getCookie('seccodeRight') === 'success') {
sendLog('verifyLoop');

setCookie('seccodeRight', 1, getUTCString(-1), location.hostname, '/');
}

if(getCookie('refresh')) {
sendLog('refresh');
}
})();

function setImgCode(code) {
try {
var t = new Date().getTime() - imgRequestTime.getTime();
sendLog('imgCost',"cost="+t);
} catch (e) {
}
window.imgCode = code;
}
sendLog('index');

function changeImg2() {
if(window.event) {
window.event.returnValue=false
}
}
</script>
</head>
<body>
<div class="header">
<div class="logo"><a href="/"><img width="180" height="60" src="//www.sogou.com/images/logo2014/error180x60.png"></a></div>
<div class="other"><span class="s1">您的访问出错了</span><span class="s2"><a href="/">返回首页&gt;&gt;</a></span></div>
</div>
<div class="content-box">
<p class="ip-time-p">IP:123.116.247.15<br>访问时间:2017.07.25 22:36:19</p>
<p class="p2">用户您好,您的访问过于频繁,为确认本次访问为正常用户行为,需要您协助验证。</p>
<p class="p3"><label for="seccodeInput">验证码:</label></p>
<form name="authform" method="POST" id="seccodeForm" action="/">
<p class="p4">
<input type=text name="c" value="" placeholder="请输入验证码" id="seccodeInput">
<input type="hidden" name="tc" id="tc" value="">
<input type="hidden" name="r" id="from" value="%2Fweixin%3Ftype%3D2%26query%3D%E9%AB%98%E8%80%83%26ie%3Dutf8%26s_from%3Dinput%26_sug_%3Dn%26_sug_type_%3D1%26w%3D01015002%26oq%3D%26ri%3D6%26sourceid%3Dsugg%26sut%3D0%26sst0%3D1500993227871%26lkt%3D0%2C0%2C0%26p%3D40040108" >
<input type="hidden" name="m" value="0" > <span class="s1">
<script>imgRequestTime=new Date();</script>
<a onclick="changeImg2();" href="javascript:void(0)">
<img id="seccodeImage" onload="setImgCode(1)" onerror="setImgCode(0)" src="util/seccode.php?tc=1500993379" width="100" height="40" alt="请输入图中的验证码" title="请输入图中的验证码">
</a>
</span>
<a href="javascript:void(0);" id="change-img" onclick="changeImg2();" style="padding-left:50px;">换一张</a>
<span class="s2" id="error-tips" style="display: none;"></span>
</p>
</form>
<p class="p5">
<a href="javascript:void(0);" id="submit">提交</a>
<span>提交后没解决问题?欢迎<a href="http://fankui.help.sogou.com/index.php/web/web/index?type=10&anti_time=1500993379&domain=weixin.sogou.com" target="_blank">反馈</a>。</span>
</p>
</div>
<div id="ft"><a href="http://fuwu.sogou.com/" target="_blank">企业推广</a><a href="http://corp.sogou.com/" target="_blank">关于搜狗</a><a href="/docs/terms.htm?v=1" target="_blank">免责声明</a><a href="http://fankui.help.sogou.com/index.php/web/web/index?type=10&anti_time=1500993379&domain=weixin.sogou.com" target="_blank">意见反馈</a><br>&nbsp;&copy;&nbsp;2017<span id="footer-year"></span>&nbsp;SOGOU&nbsp;-&nbsp;<a href="http://www.miibeian.gov.cn" target="_blank" class="g">京ICP证050897号</a>&nbsp;-&nbsp;京公网安备1100<span class="ba">00000025号</span></div>
<script src="static/js/index.min.js?v=0.1.3"></script>
</body>
</html><!--zly-->
13 changes: 13 additions & 0 deletions test/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-

import unittest


class TestAPI(unittest.TestCase):
@unittest.skip
def test_api(self):
pass


if __name__ == '__main__':
unittest.main()
4 changes: 1 addition & 3 deletions test/test_api_structuring.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
from nose.tools import assert_equal, assert_in, assert_true, assert_greater_equal

from wechatsogou.refactor_structuring import WechatSogouStructuring
from test.test_main import fake_data_path

assert_equal.__self__.maxDiff = None

ws_structuring = WechatSogouStructuring()
fake_data_path = 'test/file'


class TestStructuringGzh(unittest.TestCase):
def test_get_gzh_by_search(self):
Expand Down
File renamed without changes.
64 changes: 4 additions & 60 deletions test/test_api_request.py → test/test_gen_search_article_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

from __future__ import absolute_import, unicode_literals, print_function

import io
import os
import unittest
from nose.tools import assert_raises, assert_equal, assert_in, assert_not_equal, assert_not_in
from nose.tools import assert_raises, assert_equal, assert_in, assert_not_in

import httpretty
from hypothesis import given, strategies as st

from wechatsogou.refactor_request import WechatSogouRequest

gaokao_keyword = '高考'
ws = WechatSogouRequest()
from test.test_main import gaokao_keyword


class TestBasicGenSearchArticleURL(unittest.TestCase):
Expand All @@ -37,14 +32,14 @@ def test_gen_search_article_url_timesn(self, timesn, ft, et):
assert_in('type=2&page=1&ie=utf8&query=', url)
assert_not_in('ft=&et=', url)

url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=str(ft))
url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=ft)
assert_in('type=2&page=1&ie=utf8&query=', url)
assert_not_in('ft=&et=', url)
elif timesn in [1, 2, 3, 4]:
url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn)
assert_in('tsn={}&ft=&et='.format(timesn), url)

url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=str(ft))
url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=ft)
assert_in('tsn={}&ft=&et='.format(timesn), url)
elif timesn == 5:
if ft <= et:
Expand All @@ -70,54 +65,3 @@ def test_gen_search_article_url_article_type(self):

url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, article_type=WechatSogouRequest.TYPE_RICH)
assert_in('interation=458754%2C458756', url)


class TestBasicGenSearchGzhURL(unittest.TestCase):
def test_gen_search_article_url_keyword(self):
url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword)
assert_equal('http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E9%AB%98%E8%80%83', url)

@given(st.integers(min_value=-20000, max_value=20000))
def test_gen_search_gzh_url_page(self, page):
if page > 0:
url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword, page)
assert_in('page={}'.format(page), url)
else:
with assert_raises(AssertionError):
WechatSogouRequest._gen_search_gzh_url(gaokao_keyword, page)


class TestBasicSearchArticle(unittest.TestCase):
@httpretty.activate
def test_search_article_keyword(self):
url = WechatSogouRequest._gen_search_article_url(gaokao_keyword)
file_name = '{}/{}'.format(os.getcwd(), 'test/file/search-gaokao-article.html')
with io.open(file_name, encoding='utf-8') as f:
search_gaokao_article = f.read()
httpretty.register_uri(httpretty.GET, url, body=search_gaokao_article)

r = WechatSogouRequest._search_article(gaokao_keyword)
assert_equal(search_gaokao_article, r.text)
assert_equal(url, r.url)
assert_not_equal(WechatSogouRequest._gen_search_article_url(gaokao_keyword, 2), r.url)
assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword), r.url)


class TestBasicSearchGzh(unittest.TestCase):
@httpretty.activate
def test_search_gzh_keyword(self):
url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword)
file_name = '{}/{}'.format(os.getcwd(), 'test/file/search-gaokao-gzh.html')
with io.open(file_name, encoding='utf-8') as f:
search_gaokao_gzh = f.read()
httpretty.register_uri(httpretty.GET, url, body=search_gaokao_gzh)

r = WechatSogouRequest._search_gzh(gaokao_keyword)
assert_equal(search_gaokao_gzh, r.text)
assert_equal(url, r.url)
assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword, 2), r.url)
assert_not_equal(WechatSogouRequest._search_article(gaokao_keyword), r.url)


if __name__ == '__main__':
unittest.main()
26 changes: 26 additions & 0 deletions test/test_gen_search_gzh_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals, print_function

import unittest
from nose.tools import assert_raises, assert_equal, assert_in

from hypothesis import given, strategies as st

from wechatsogou.refactor_request import WechatSogouRequest
from test.test_main import gaokao_keyword


class TestBasicGenSearchGzhURL(unittest.TestCase):
def test_gen_search_article_url_keyword(self):
url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword)
assert_equal('http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E9%AB%98%E8%80%83', url)

@given(st.integers(min_value=-20000, max_value=20000))
def test_gen_search_gzh_url_page(self, page):
if page > 0:
url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword, page)
assert_in('page={}'.format(page), url)
else:
with assert_raises(AssertionError):
WechatSogouRequest._gen_search_gzh_url(gaokao_keyword, page)
12 changes: 12 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals, print_function

from wechatsogou.refactor_request import WechatSogouRequest
from wechatsogou.refactor_structuring import WechatSogouStructuring

ws = WechatSogouRequest()
ws_structuring = WechatSogouStructuring()

gaokao_keyword = '高考'
fake_data_path = 'test/file'
65 changes: 65 additions & 0 deletions test/test_search_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals, print_function

import io
import os
import unittest
from nose.tools import assert_equal, assert_not_equal

import httpretty

from wechatsogou.refactor_request import WechatSogouRequest
from test.test_main import gaokao_keyword, fake_data_path


class TestBasicSearchArticle(unittest.TestCase):
@httpretty.activate
def test_search_article_keyword(self):
url = WechatSogouRequest._gen_search_article_url(gaokao_keyword)
file_name = '{}/{}/{}'.format(os.getcwd(), fake_data_path, 'search-gaokao-article.html')
with io.open(file_name, encoding='utf-8') as f:
search_gaokao_article = f.read()
httpretty.register_uri(httpretty.GET, url, body=search_gaokao_article)

r = WechatSogouRequest._search_article(gaokao_keyword)
assert_equal(search_gaokao_article, r.text)
assert_equal(url, r.url)
assert_not_equal(WechatSogouRequest._gen_search_article_url(gaokao_keyword, 2), r.url)
assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword), r.url)


class TestBasicSearchGzh(unittest.TestCase):
@httpretty.activate
def test_search_gzh_keyword(self):
url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword)
file_name = '{}/{}/{}'.format(os.getcwd(), fake_data_path, 'search-gaokao-gzh.html')
with io.open(file_name, encoding='utf-8') as f:
search_gaokao_gzh = f.read()
httpretty.register_uri(httpretty.GET, url, body=search_gaokao_gzh)

r = WechatSogouRequest._search_gzh(gaokao_keyword)
assert_equal(search_gaokao_gzh, r.text)
assert_equal(url, r.url)
assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword, 2), r.url)
assert_not_equal(WechatSogouRequest._search_article(gaokao_keyword), r.url)

@httpretty.activate
def test_search_gzh_keyword_error(self):
url = WechatSogouRequest._gen_search_gzh_url(gaokao_keyword)
file_name = '{}/{}/{}'.format(os.getcwd(), fake_data_path, 'search-gaokao-gzh-error.html')
with io.open(file_name, encoding='utf-8') as f:
search_gaokao_gzh_error = f.read()
httpretty.register_uri(httpretty.GET, url, body=search_gaokao_gzh_error)

r = WechatSogouRequest._search_gzh(gaokao_keyword)
assert_equal(search_gaokao_gzh_error, r.text)
assert_equal(url, r.url)
assert_not_equal(WechatSogouRequest._search_gzh(gaokao_keyword, 2), r.url)
assert_not_equal(WechatSogouRequest._search_article(gaokao_keyword), r.url)




if __name__ == '__main__':
unittest.main()
7 changes: 7 additions & 0 deletions wechatsogou/refactor_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals, print_function


class WechatSogouAPI(object):
pass
4 changes: 2 additions & 2 deletions wechatsogou/refactor_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _search_article(keyword, page=1, timesn=0, article_type=None, wxid=None, usi

Returns
-------
requests
requests.models.Response
return of requests
"""

Expand All @@ -162,7 +162,7 @@ def _search_gzh(keyword, page=1):

Returns
-------
requests
requests.models.Response
return of requests
"""
url = WechatSogouRequest._gen_search_gzh_url(keyword, page)
Expand Down