def test_badrequest(self): """测试错误请求""" nonce = "123456" # 错误时间戳 query = dict( timestamp=str(int(time.time() - 1000)), nonce=nonce ) resp = self.post(query) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.content, b"") settings.MESSAGETIMEOFFSET = 2000 resp = self.post(query) self.assertEqual(resp.status_code, 200) reply = deserialize_reply(resp.content) self.assertEqual(reply.content, self.success_reply) # 错误签名 query["signature"] = "666666" resp = self.post(query) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.content, b"") # 错误格式 del query["signature"] resp = self.post(query) self.assertEqual(resp.status_code, 200) reply = deserialize_reply(resp.content) self.assertEqual(reply.content, self.success_reply) resp = self.client.post(self.url + "?" + urlencode(query), dict(a=1)) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.content, b"") # 防重放 settings.MESSAGENOREPEATNONCE = True resp = self.post(query) self.assertEqual(resp.status_code, 200) reply = deserialize_reply(resp.content) self.assertEqual(reply.content, self.success_reply) resp = self.post(query) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.content, b"") # 防重放超时正常接收 pass
def reply_forward(self, message_info): """ :type message_info: wechat_django.models.WeChatMessageInfo """ resp = requests.post(self.content["url"], message_info.raw, params=message_info.request.GET, timeout=4.5) resp.raise_for_status() return replies.deserialize_reply(resp.content)
def test_request(self): """测试正常请求""" timestamp = str(int(time.time())) nonce = "123456" query = dict(timestamp=timestamp, nonce=nonce) # 未匹配到 resp = self.post(query, "666") self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content, b"") # 正常匹配 resp = self.post(query) self.assertEqual(resp.status_code, 200) reply = deserialize_reply(resp.content) self.assertEqual(reply.target, self.sender) self.assertIsInstance(reply, TextReply) self.assertEqual(reply.content, self.success_reply)
def _test_deserialize(self, reply): xml = reply.render() deserialized = replies.deserialize_reply(xml) xml2 = deserialized.render() self.assertEqual(xml, xml2)