View Javadoc
1   package com.foxinmy.weixin4j.server.test;
2   
3   import java.io.IOException;
4   
5   import org.junit.Assert;
6   import org.junit.Test;
7   
8   /**
9    * 接受一般消息格式测试
10   * 
11   * @author jinyu(foxinmy@gmail.com)
12   * @date 2014年3月24日
13   * @since JDK 1.6
14   */
15  public class MessageRequestTest extends MessagePush {
16  
17  	private StringBuilder xmlSb = new StringBuilder();
18  
19  	/***************** common message *********************/
20  
21  	@Test
22  	public void text() throws IOException {
23  		String para = "?signature=d919cc8a6361597afa536e906156262cc9cd93df&timestamp=1433903433&nonce=518016546";
24  		xmlSb.delete(0, xmlSb.length());
25  		xmlSb.append("<xml>");
26  		xmlSb.append("<ToUserName><![CDATA[toUser]]></ToUserName>");
27  		xmlSb.append("<FromUserName><![CDATA[fromUser]]></FromUserName>");
28  		xmlSb.append("<CreateTime>1348831860</CreateTime>");
29  		xmlSb.append("<MsgType><![CDATA[text]]></MsgType>");
30  		xmlSb.append("<Content><![CDATA[this is a test]]></Content>");
31  		xmlSb.append("<MsgId>1234567890123456</MsgId>");
32  		xmlSb.append("</xml>");
33  		String response = push(para, xmlSb.toString());
34  		Assert.assertNotNull(response);
35  		System.out.println(response);
36  	}
37  
38  	@Test
39  	public void image() throws IOException {
40  		String para = "?signature=d919cc8a6361597afa536e906156262cc9cd93df&timestamp=1433903433&nonce=518016546";
41  		xmlSb.delete(0, xmlSb.length());
42  		xmlSb.append("<xml>");
43  		xmlSb.append("<ToUserName><![CDATA[toUser]]></ToUserName>");
44  		xmlSb.append("<FromUserName><![CDATA[FromUser]]></FromUserName>");
45  		xmlSb.append("<CreateTime>123456789</CreateTime>");
46  		xmlSb.append("<MsgType><![CDATA[image]]></MsgType>");
47  		xmlSb.append("<PicUrl><![CDATA[this is a url]]></PicUrl>");
48  		xmlSb.append("<MediaId><![CDATA[media_id]]></MediaId>");
49  		xmlSb.append("<MsgId>1234567890123456</MsgId>");
50  		xmlSb.append("</xml>");
51  		String response = push(para, xmlSb.toString());
52  		Assert.assertNotNull(response);
53  		System.out.println(response);
54  	}
55  
56  	@Test
57  	public void voice() throws IOException {
58  		String para = "?signature=d919cc8a6361597afa536e906156262cc9cd93df&timestamp=1433903433&nonce=518016546";
59  		xmlSb.delete(0, xmlSb.length());
60  		xmlSb.append("<xml>");
61  		xmlSb.append("<ToUserName><![CDATA[toUser]]></ToUserName>");
62  		xmlSb.append("<FromUserName><![CDATA[FromUser]]></FromUserName>");
63  		xmlSb.append("<CreateTime>123456789</CreateTime>");
64  		xmlSb.append("<MsgType><![CDATA[voice]]></MsgType>");
65  		xmlSb.append("<MediaId><![CDATA[media_id]]></MediaId>");
66  		xmlSb.append("<Format><![CDATA[Format]]></Format>");
67  		xmlSb.append("<MsgId>1234567890123456</MsgId>");
68  		xmlSb.append("</xml>");
69  		String response = push(para, xmlSb.toString());
70  		Assert.assertNotNull(response);
71  		System.out.println(response);
72  	}
73  
74  	@Test
75  	public void re_voice() throws IOException {
76  		String para = "?signature=d919cc8a6361597afa536e906156262cc9cd93df&timestamp=1433903433&nonce=518016546";
77  		xmlSb.delete(0, xmlSb.length());
78  		xmlSb.append("<xml>");
79  		xmlSb.append("<ToUserName><![CDATA[toUser]]></ToUserName>");
80  		xmlSb.append("<FromUserName><![CDATA[FromUser]]></FromUserName>");
81  		xmlSb.append("<CreateTime>123456789</CreateTime>");
82  		xmlSb.append("<MsgType><![CDATA[voice]]></MsgType>");
83  		xmlSb.append("<MediaId><![CDATA[media_id]]></MediaId>");
84  		xmlSb.append("<Format><![CDATA[Format]]></Format>");
85  		xmlSb.append("<Recognition><![CDATA[腾讯微信团队]]></Recognition>");
86  		xmlSb.append("<MsgId>1234567890123456</MsgId>");
87  		xmlSb.append("</xml>");
88  		String response = push(para, xmlSb.toString());
89  		Assert.assertNotNull(response);
90  		System.out.println(response);
91  	}
92  
93  	@Test
94  	public void video() throws IOException {
95  		String para = "?signature=d919cc8a6361597afa536e906156262cc9cd93df&timestamp=1433903433&nonce=518016546";
96  		xmlSb.delete(0, xmlSb.length());
97  		xmlSb.append("<xml>");
98  		xmlSb.append("<ToUserName><![CDATA[toUser]]></ToUserName>");
99  		xmlSb.append("<FromUserName><![CDATA[FromUser]]></FromUserName>");
100 		xmlSb.append("<CreateTime>123456789</CreateTime>");
101 		xmlSb.append("<MsgType><![CDATA[video]]></MsgType>");
102 		xmlSb.append("<MediaId><![CDATA[media_id]]></MediaId>");
103 		xmlSb.append("<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>");
104 		xmlSb.append("<MsgId>1234567890123456</MsgId>");
105 		xmlSb.append("</xml>");
106 		String response = push(para, xmlSb.toString());
107 		Assert.assertNotNull(response);
108 		System.out.println(response);
109 	}
110 
111 	@Test
112 	public void location() throws IOException {
113 		String para = "?signature=d919cc8a6361597afa536e906156262cc9cd93df&timestamp=1433903433&nonce=518016546";
114 		xmlSb.delete(0, xmlSb.length());
115 		xmlSb.append("<xml>");
116 		xmlSb.append("<ToUserName><![CDATA[toUser]]></ToUserName>");
117 		xmlSb.append("<FromUserName><![CDATA[FromUser]]></FromUserName>");
118 		xmlSb.append("<CreateTime>123456789</CreateTime>");
119 		xmlSb.append("<MsgType><![CDATA[location]]></MsgType>");
120 		xmlSb.append("<Location_X>23.134521</Location_X>");
121 		xmlSb.append("<Location_Y>113.358803</Location_Y>");
122 		xmlSb.append("<Scale>20</Scale>");
123 		xmlSb.append("<Label><![CDATA[位置信息]]></Label>");
124 		xmlSb.append("<MsgId>1234567890123456</MsgId>");
125 		xmlSb.append("</xml>");
126 		String response = push(para, xmlSb.toString());
127 		Assert.assertNotNull(response);
128 		System.out.println(response);
129 	}
130 
131 	@Test
132 	public void link() throws IOException {
133 		String para = "?signature=d919cc8a6361597afa536e906156262cc9cd93df&timestamp=1433903433&nonce=518016546";
134 		xmlSb.delete(0, xmlSb.length());
135 		xmlSb.append("<xml>");
136 		xmlSb.append("<ToUserName><![CDATA[toUser]]></ToUserName>");
137 		xmlSb.append("<FromUserName><![CDATA[FromUser]]></FromUserName>");
138 		xmlSb.append("<CreateTime>123456789</CreateTime>");
139 		xmlSb.append("<MsgType><![CDATA[link]]></MsgType>");
140 		xmlSb.append("<Title><![CDATA[公众平台官网链接]]></Title>");
141 		xmlSb.append("<Description><![CDATA[公众平台官网链接]]></Description>");
142 		xmlSb.append("<Url><![CDATA[url]]></Url>");
143 		xmlSb.append("<MsgId>1234567890123456</MsgId>");
144 		xmlSb.append("</xml>");
145 		String response = push(para, xmlSb.toString());
146 		Assert.assertNotNull(response);
147 		System.out.println(response);
148 	}
149 }