Example #1
0
func TestResponseXMLandJSONMarshal(t *testing.T) {
	for _, test := range responseMarshalTests {
		// xml
		b, err := xml.Marshal(test.Value)
		if err != nil {
			t.Errorf("xml.Marshal(%#v):\nError: %s\n", test.Value, err)
			continue
		}

		want := util.TrimSpace(test.ExpectXML)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#v):\nhave %#s\nwant %#s\n", test.Value, b, want)
			continue
		}

		// json
		b, err = json.Marshal(test.Value)
		if err != nil {
			t.Errorf("json.Marshal(%#v):\nError: %s\n", test.Value, err)
			continue
		}
		want = util.TrimSpace(test.ExpectJSON)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#v):\nhave %#s\nwant %#s\n", test.Value, b, want)
			continue
		}
	}
}
Example #2
0
func TestGroupProductModifyRequestNewAndAppend(t *testing.T) {
	expectBytes := []byte(`{
		"group_id":28,
		"product":[
			{
				"product_id":"pDF3iY-CgqlAL3k8Ilz-6sj0UYpk",
				"mod_action":1
			},
			{
				"product_id":"pDF3iY-RewlAL3k8Ilz-6sjsepp9",
				"mod_action":0
			}
		]
	}`)

	request := NewGroupProductModifyRequest(
		28,
		[]string{"pDF3iY-CgqlAL3k8Ilz-6sj0UYpk"},
		[]string{"pDF3iY-RewlAL3k8Ilz-6sjsepp9"},
	)

	b, err := json.Marshal(request)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", request, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", request, b, want)
		}
	}

	request = NewGroupProductModifyRequest(28, nil, nil)
	request.AddProduct("pDF3iY-CgqlAL3k8Ilz-6sj0UYpk")
	request.DeleteProduct("pDF3iY-RewlAL3k8Ilz-6sjsepp9")

	b, err = json.Marshal(request)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", request, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", request, b, want)
		}
	}
}
Example #3
0
func TestMarshalAndNewFunc(t *testing.T) {

	// 测试文本消息===============================================================

	want := util.TrimSpace([]byte(`{
	    "filter":{
	        "group_id":"2"
	    }, 
	    "msgtype":"text", 
	    "text":{
	        "content":"CONTENT"
	    }
	}`))

	text := NewText(2, "CONTENT")

	have, err := json.Marshal(text)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", text, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", text, have, want)
	}

	// 测试图片消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "filter":{
	        "group_id":"2"
	    }, 
	    "msgtype":"image", 
	    "image":{
	        "media_id":"123dsdajkasd231jhksad"
	    }
	}`))

	image := NewImage(2, "123dsdajkasd231jhksad")

	have, err = json.Marshal(image)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", image, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", image, have, want)
	}

	// 测试语音消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "filter":{
	        "group_id":"2"
	    }, 
	    "msgtype":"voice", 
	    "voice":{
	        "media_id":"123dsdajkasd231jhksad"
	    }
	}`))

	voice := NewVoice(2, "123dsdajkasd231jhksad")

	have, err = json.Marshal(voice)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", voice, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", voice, have, want)
	}

	// 测试视频消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "filter":{
	        "group_id":"2"
	    }, 
	    "msgtype":"mpvideo", 
	    "mpvideo":{
	        "media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc"
	    }
	}`))

	video := NewVideo(2, "IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc")

	have, err = json.Marshal(video)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", video, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", video, have, want)
	}

	// 测试图文消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "filter":{
	        "group_id":"2"
	    }, 
	    "msgtype":"mpnews", 
	    "mpnews":{
	        "media_id":"123dsdajkasd231jhksad"
	    }
	}`))

	news := NewNews(2, "123dsdajkasd231jhksad")

	have, err = json.Marshal(news)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", news, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", news, have, want)
	}
}
Example #4
0
func TestMarshalAndNewFunc(t *testing.T) {

	// 测试文本消息===============================================================

	want := util.TrimSpace([]byte(`{
	    "touser":[
	        "oR5Gjjl_eiZoUpGozMo7dbBJ362A", 
	        "oR5Gjjo5rXlMUocSEXKT7Q5RQ63Q"
	    ], 
	    "msgtype":"text", 
	    "text":{
	        "content":"hello from boxer."
	    }
	}`))

	text := NewText(
		[]string{"oR5Gjjl_eiZoUpGozMo7dbBJ362A", "oR5Gjjo5rXlMUocSEXKT7Q5RQ63Q"},
		"hello from boxer.",
	)

	have, err := json.Marshal(text)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", text, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", text, have, want)
	}

	// 测试图片消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":[
	        "OPENID1", 
	        "OPENID2"
	    ], 
	    "msgtype":"image", 
	    "image":{
	        "media_id":"BTgN0opcW3Y5zV_ZebbsD3NFKRWf6cb7OPswPi9Q83fOJHK2P67dzxn11Cp7THat"
	    }
	}`))

	image := NewImage([]string{"OPENID1", "OPENID2"}, "BTgN0opcW3Y5zV_ZebbsD3NFKRWf6cb7OPswPi9Q83fOJHK2P67dzxn11Cp7THat")

	have, err = json.Marshal(image)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", image, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", image, have, want)
	}

	// 测试语音消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":[
	        "OPENID1", 
	        "OPENID2"
	    ], 
	    "msgtype":"voice", 
	    "voice":{
	        "media_id":"mLxl6paC7z2Tl-NJT64yzJve8T9c8u9K2x-Ai6Ujd4lIH9IBuF6-2r66mamn_gIT"
	    }
	}`))

	voice := NewVoice([]string{"OPENID1", "OPENID2"}, "mLxl6paC7z2Tl-NJT64yzJve8T9c8u9K2x-Ai6Ujd4lIH9IBuF6-2r66mamn_gIT")

	have, err = json.Marshal(voice)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", voice, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", voice, have, want)
	}

	// 测试视频消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":[
	        "OPENID1", 
	        "OPENID2"
	    ], 
	    "msgtype":"video", 
	    "video":{
	        "media_id":"123dsdajkasd231jhksad", 
	        "title":"TITLE", 
	        "description":"DESCRIPTION"
	    }
	}`))

	video := NewVideo(
		[]string{"OPENID1", "OPENID2"},
		"123dsdajkasd231jhksad",
		"TITLE",
		"DESCRIPTION",
	)

	have, err = json.Marshal(video)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", video, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", video, have, want)
	}

	// 测试图文消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":[
	        "OPENID1", 
	        "OPENID2"
	    ], 
	    "msgtype":"mpnews", 
	    "mpnews":{
	        "media_id":"123dsdajkasd231jhksad"
	    }
	}`))

	news := NewNews([]string{"OPENID1", "OPENID2"}, "123dsdajkasd231jhksad")

	have, err = json.Marshal(news)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", news, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", news, have, want)
	}
}
Example #5
0
func TestMarshalAndNewFunc(t *testing.T) {
	var expectBytes []byte

	// 测试文本消息===============================================================

	expectBytes = []byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>text</MsgType>
		<Content>你好</Content>
	</xml>`)

	text := NewText("toUser", "fromUser", "你好")
	text.CreateTime = 12345678

	b, err := xml.Marshal(text)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", text, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", text, b, want)
		}
	}

	// 测试图片消息===============================================================

	expectBytes = []byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>image</MsgType>
		<Image>
			<MediaId>media_id</MediaId>
		</Image>
	</xml>`)

	image := NewImage("toUser", "fromUser", "media_id")
	image.CreateTime = 12345678

	b, err = xml.Marshal(image)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", image, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", image, b, want)
		}
	}

	// 测试语音消息===============================================================

	expectBytes = []byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>voice</MsgType>
		<Voice>
			<MediaId>media_id</MediaId>
		</Voice>
	</xml>`)

	voice := NewVoice("toUser", "fromUser", "media_id")
	voice.CreateTime = 12345678

	b, err = xml.Marshal(voice)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", voice, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", voice, b, want)
		}
	}

	// 测试视频消息===============================================================

	expectBytes = []byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>video</MsgType>
		<Video>
			<MediaId>media_id</MediaId>
			<Title>title</Title>
			<Description>description</Description>
		</Video> 
	</xml>`)

	video := NewVideo("toUser", "fromUser", "media_id", "title", "description")
	video.CreateTime = 12345678

	b, err = xml.Marshal(video)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", video, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", video, b, want)
		}
	}

	// 测试音乐消息===============================================================

	expectBytes = []byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>music</MsgType>
		<Music>
			<Title>TITLE</Title>
			<Description>DESCRIPTION</Description>
			<MusicUrl>MUSIC_Url</MusicUrl>
			<HQMusicUrl>HQ_MUSIC_Url</HQMusicUrl>
			<ThumbMediaId>media_id</ThumbMediaId>
		</Music>
	</xml>`)

	music := NewMusic("toUser", "fromUser", "media_id", "MUSIC_Url", "HQ_MUSIC_Url", "TITLE", "DESCRIPTION")
	music.CreateTime = 12345678

	b, err = xml.Marshal(music)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", music, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", music, b, want)
		}
	}

	// 测试图文消息===============================================================

	// 没有文章
	expectBytes = []byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>news</MsgType>
		<ArticleCount>0</ArticleCount>
		<Articles>
		</Articles>
	</xml>`)

	news := NewNews("toUser", "fromUser", nil)
	news.CreateTime = 12345678

	b, err = xml.Marshal(news)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", news, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", news, b, want)
		}
	}

	// 增加一篇没有文章
	expectBytes = []byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>news</MsgType>
		<ArticleCount>1</ArticleCount>
		<Articles>
			<item>
				<Title>title1</Title> 
				<Description>description1</Description>
				<PicUrl>picurl</PicUrl>
				<Url>url</Url>
			</item>
		</Articles>
	</xml>`)

	news.AppendArticle(&NewsArticle{
		Title:       "title1",
		Description: "description1",
		PicURL:      "picurl",
		URL:         "url",
	})

	b, err = xml.Marshal(news)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", news, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", news, b, want)
		}
	}

	// 再增加一篇没有文章
	expectBytes = []byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>news</MsgType>
		<ArticleCount>2</ArticleCount>
		<Articles>
			<item>
				<Title>title1</Title> 
				<Description>description1</Description>
				<PicUrl>picurl</PicUrl>
				<Url>url</Url>
			</item>
			<item>
				<Title>title</Title>
				<Description>description</Description>
				<PicUrl>picurl</PicUrl>
				<Url>url</Url>
			</item>
		</Articles>
	</xml>`)

	news.AppendArticle(&NewsArticle{
		Title:       "title",
		Description: "description",
		PicURL:      "picurl",
		URL:         "url",
	})

	b, err = xml.Marshal(news)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", news, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", news, b, want)
		}
	}

	// 测试将消息转发到多客服=======================================================
	expectBytes = []byte(`<xml>
		<ToUserName>touser</ToUserName>
		<FromUserName>fromuser</FromUserName>
		<CreateTime>1399197672</CreateTime>
		<MsgType>transfer_customer_service</MsgType>
	</xml>`)

	transToCS := NewTransferCustomerService("touser", "fromuser")
	transToCS.CreateTime = 1399197672

	b, err = xml.Marshal(transToCS)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", transToCS, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", transToCS, b, want)
		}
	}
}
Example #6
0
func TestMarshalAndNewFunc(t *testing.T) {
	var expectBytes []byte

	// 测试文本消息===============================================================

	expectBytes = []byte(`{
		"touser":"******",
		"msgtype":"text",
		"text":
		{
			"content":"你好"
		}
	}`)

	text := NewText("toUser", "你好")

	b, err := json.Marshal(text)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", text, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", text, b, want)
		}
	}

	// 测试图片消息===============================================================

	expectBytes = []byte(`{
		"touser":"******",
		"msgtype":"image",
		"image":
		{
			"media_id":"media_id"
		}
	}`)

	image := NewImage("toUser", "media_id")

	b, err = json.Marshal(image)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", image, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", image, b, want)
		}
	}

	// 测试语音消息===============================================================

	expectBytes = []byte(`{
		"touser":"******",
		"msgtype":"voice",
		"voice":
		{
			"media_id":"media_id"
		}
	}`)

	voice := NewVoice("toUser", "media_id")

	b, err = json.Marshal(voice)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", voice, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", voice, b, want)
		}
	}

	// 测试视频消息===============================================================

	expectBytes = []byte(`{
		"touser":"******",
		"msgtype":"video",
		"video":
		{
			"media_id":"media_id",
			"title":"title",
			"description":"description"
		}
	}`)

	video := NewVideo("toUser", "media_id", "title", "description")

	b, err = json.Marshal(video)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", video, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", video, b, want)
		}
	}

	// 测试音乐消息===============================================================

	expectBytes = []byte(`{
		"touser":"******",
		"msgtype":"music",
		"music":
		{
			"title":"TITLE",
			"description":"DESCRIPTION",
			"musicurl":"MUSIC_Url",
			"hqmusicurl":"HQ_MUSIC_Url",
			"thumb_media_id":"media_id" 
		}
	}`)

	music := NewMusic("toUser", "media_id", "MUSIC_Url", "HQ_MUSIC_Url", "TITLE", "DESCRIPTION")

	b, err = json.Marshal(music)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", music, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", music, b, want)
		}
	}

	// 测试图文消息===============================================================

	// 没有文章
	expectBytes = []byte(`{
		"touser":"******",
		"msgtype":"news",
		"news":{}
	}`)

	news := NewNews("toUser", nil)

	b, err = json.Marshal(news)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", news, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", news, b, want)
		}
	}

	// 增加一篇没有文章
	expectBytes = []byte(`{
		"touser":"******",
		"msgtype":"news",
		"news":{
			"articles":[
				{
					"title":"title1",
					"description":"description1",
					"picurl":"picurl",
					"url":"url"
				}
			]
		}
	}`)

	news.News.Articles = append(
		news.News.Articles,
		&NewsArticle{
			Title:       "title1",
			Description: "description1",
			PicURL:      "picurl",
			URL:         "url",
		},
	)

	b, err = json.Marshal(news)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", news, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", news, b, want)
		}
	}

	// 再增加一篇没有文章
	expectBytes = []byte(`{
		"touser":"******",
		"msgtype":"news",
		"news":{
			"articles":[
				{
					"title":"title1",
					"description":"description1",
					"picurl":"picurl",
					"url":"url"
				},
				{
					"title":"title",
					"description":"description",
					"picurl":"picurl",
					"url":"url"
				}
			]
		}
	}`)

	news.News.Articles = append(
		news.News.Articles,
		&NewsArticle{
			Title:       "title",
			Description: "description",
			PicURL:      "picurl",
			URL:         "url",
		},
	)

	b, err = json.Marshal(news)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", news, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", news, b, want)
		}
	}
}
Example #7
0
func TestMenuJSONMarshal(t *testing.T) {
	want := util.TrimSpace([]byte(`{
		"button":[
			{
				"type":"click", 
				"name":"今日歌曲", 
				"key":"V1001_TODAY_MUSIC"
			}, 
			{
				"name":"菜单", 
				"sub_button":[
					{
						"type":"view", 
						"name":"搜索", 
						"url":"http://www.soso.com/"
					}, 
					{
						"type":"view", 
						"name":"视频", 
						"url":"http://v.qq.com/"
					}, 
					{
						"type":"click", 
						"name":"赞一下我们", 
						"key":"V1001_GOOD"
					}
				]
			}
		]
	}`))

	var subButtons = make([]Button, 3)
	subButtons[0].InitToViewButton("搜索", "http://www.soso.com/")
	subButtons[1].InitToViewButton("视频", "http://v.qq.com/")
	subButtons[2].InitToClickButton("赞一下我们", "V1001_GOOD")

	var mn Menu
	mn.Buttons = make([]Button, 2)
	mn.Buttons[0].InitToClickButton("今日歌曲", "V1001_TODAY_MUSIC")
	mn.Buttons[1].InitToSubMenuButton("菜单", subButtons)

	have, err := json.Marshal(mn)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", mn, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", mn, have, want)
	}

	want = util.TrimSpace([]byte(`{
		"button":[
			{
				"name":"扫码", 
				"sub_button":[
					{
						"type":"scancode_waitmsg", 
						"name":"扫码带提示", 
						"key":"rselfmenu_0_0"
					}, 
					{
						"type":"scancode_push", 
						"name":"扫码推事件", 
						"key":"rselfmenu_0_1"
					}
				]
			}, 
			{
				"name":"发图", 
				"sub_button":[
					{
						"type":"pic_sysphoto", 
						"name":"系统拍照发图", 
						"key":"rselfmenu_1_0"
					}, 
					{
						"type":"pic_photo_or_album", 
						"name":"拍照或者相册发图", 
						"key":"rselfmenu_1_1"
					}, 
					{
						"type":"pic_weixin", 
						"name":"微信相册发图", 
						"key":"rselfmenu_1_2"
					}
				]
			}, 
			{
				"type":"location_select",
				"name":"发送位置", 
				"key":"rselfmenu_2_0"
			}
		]
	}`))

	var subButtons0 = make([]Button, 2)
	subButtons0[0].InitToScanCodeWaitMsgButton("扫码带提示", "rselfmenu_0_0")
	subButtons0[1].InitToScanCodePushButton("扫码推事件", "rselfmenu_0_1")

	var subButtons1 = make([]Button, 3)
	subButtons1[0].InitToPicSysPhotoButton("系统拍照发图", "rselfmenu_1_0")
	subButtons1[1].InitToPicPhotoOrAlbumButton("拍照或者相册发图", "rselfmenu_1_1")
	subButtons1[2].InitToPicWeixinButton("微信相册发图", "rselfmenu_1_2")

	var mn1 Menu
	mn1.Buttons = make([]Button, 3)
	mn1.Buttons[0].InitToSubMenuButton("扫码", subButtons0)
	mn1.Buttons[1].InitToSubMenuButton("发图", subButtons1)
	mn1.Buttons[2].InitToLocationSelectButton("发送位置", "rselfmenu_2_0")

	have, err = json.Marshal(mn1)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", mn1, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", mn1, have, want)
	}
}
Example #8
0
func TestShelfMarshal(t *testing.T) {
	expectBytes := []byte(`{
		"shelf_name":"测试货架",
		"shelf_banner":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2ibrWQn8zWFUh1YznsMV0XEiavFfLzDWYyvQOBBszXlMaiabGWzz5B2KhNn2IDemHa3iarmCyribYlZYyw/0",
		"shelf_info":{
			"module_infos":[
				{
					"eid":1,
					"group_info":{
						"group_id":50,
						"filter":{
							"count":2
						}
					}
				},
				{
					"eid":2,
					"group_infos":{
						"groups":[
							{
								"group_id":49
							},
							{
								"group_id":50
							},
							{
								"group_id":51
							}
						]
					}
				},
				{
					"eid":3,
					"group_info":{
						"group_id":52,
						"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5Jm64z4I0TTicv0TjN7Vl9bykUUibYKIOjicAwIt6Oy0Y6a1Rjp5Tos8tg/0"
					}
				},
				{
					"eid":4,
					"group_infos":{
						"groups":[
							{
								"group_id":49,
								"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
							},
							{
								"group_id":50,
								"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5G1kdy3ViblHrR54gbCmbiaMnl5HpLGm5JFeENyO9FEZAy6mPypEpLibLA/0"
							},
							{
								"group_id":52,
								"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
							}
						]
					}
				},
				{
					"eid":5,
					"group_infos":{
						"groups":[
							{
								"group_id":43
							},
							{
								"group_id":44
							},
							{
								"group_id":45
							},
							{
								"group_id":46
							}
						],
						"img_background":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
					}
				}
			]
		}
	}`)

	_shelf := Shelf{
		Name:   "测试货架",
		Banner: "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2ibrWQn8zWFUh1YznsMV0XEiavFfLzDWYyvQOBBszXlMaiabGWzz5B2KhNn2IDemHa3iarmCyribYlZYyw/0",
	}
	modules := make([]Module, 5)
	modules[0].InitToModule1(50, 2)
	modules[1].InitToModule2([]int64{49, 50, 51})
	modules[2].InitToModule3(52, "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5Jm64z4I0TTicv0TjN7Vl9bykUUibYKIOjicAwIt6Oy0Y6a1Rjp5Tos8tg/0")
	modules[3].InitToModule4([]Group{
		Group{
			GroupId: 49,
			Image:   "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0",
		},
		Group{
			GroupId: 50,
			Image:   "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5G1kdy3ViblHrR54gbCmbiaMnl5HpLGm5JFeENyO9FEZAy6mPypEpLibLA/0",
		},
		Group{
			GroupId: 52,
			Image:   "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0",
		},
	})
	modules[4].InitToModule5([]int64{43, 44, 45, 46}, "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0")
	_shelf.Info.ModuleInfos = modules

	b, err := json.Marshal(_shelf)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", _shelf, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", _shelf, b, want)
		}
	}
}
Example #9
0
func TestGroupProductModifyRequestNewAndAppend(t *testing.T) {
	expectBytes := []byte(`{
		"Name":"testexpress",
		"Assumer":0,
		"Valuation":0,
		"TopFee":[
			{
				"Type":10000027,
				"Normal":{
					"StartStandards":1,
					"StartFees":2,
					"AddStandards":3,
					"AddFees":1
				},
				"Custom":[
					{
						"StartStandards":1,
						"StartFees":100,
						"AddStandards":1,
						"AddFees":3,
						"DestCountry":"中国",
						"DestProvince":"广东省",
						"DestCity":"广州市"
					}
				]
			},
			{
				"Type":10000028,
				"Normal":{
					"StartStandards":1,
					"StartFees":3,
					"AddStandards":3,
					"AddFees":2
				},
				"Custom":[
					{
						"StartStandards":1,
						"StartFees":10,
						"AddStandards":1,
						"AddFees":30,
						"DestCountry":"中国",
						"DestProvince":"广东省",
						"DestCity":"广州市"
					}
				]
			},
			{
				"Type":10000029,
				"Normal":{
					"StartStandards":1,
					"StartFees":4,
					"AddStandards":3,
					"AddFees":3
				},
				"Custom":[
					{
						"StartStandards":1,
						"StartFees":8,
						"AddStandards":2,
						"AddFees":11,
						"DestCountry":"中国",
						"DestProvince":"广东省",
						"DestCity":"广州市"
					}
				]
			}
		]
	}`)

	tpl := DeliveryTemplate{
		Name:      "testexpress",
		Assumer:   ASSUMER_BUYER,
		Valuation: VALUATION_BY_ITEM,
		TopFees: []TopFee{
			TopFee{
				ExpressId: 10000027,
				Normal: TopFeeNormal{
					StartStandards: 1,
					StartFees:      2,
					AddStandards:   3,
					AddFees:        1,
				},
				Customs: []TopFeeCustom{
					TopFeeCustom{
						TopFeeNormal: TopFeeNormal{
							StartStandards: 1,
							StartFees:      100,
							AddStandards:   1,
							AddFees:        3,
						},
						DestCountry:  "中国",
						DestProvince: "广东省",
						DestCity:     "广州市",
					},
				},
			},
			TopFee{
				ExpressId: 10000028,
				Normal: TopFeeNormal{
					StartStandards: 1,
					StartFees:      3,
					AddStandards:   3,
					AddFees:        2,
				},
				Customs: []TopFeeCustom{
					TopFeeCustom{
						TopFeeNormal: TopFeeNormal{
							StartStandards: 1,
							StartFees:      10,
							AddStandards:   1,
							AddFees:        30,
						},
						DestCountry:  "中国",
						DestProvince: "广东省",
						DestCity:     "广州市",
					},
				},
			},
			TopFee{
				ExpressId: 10000029,
				Normal: TopFeeNormal{
					StartStandards: 1,
					StartFees:      4,
					AddStandards:   3,
					AddFees:        3,
				},
				Customs: []TopFeeCustom{
					TopFeeCustom{
						TopFeeNormal: TopFeeNormal{
							StartStandards: 1,
							StartFees:      8,
							AddStandards:   2,
							AddFees:        11,
						},
						DestCountry:  "中国",
						DestProvince: "广东省",
						DestCity:     "广州市",
					},
				},
			},
		},
	}

	b, err := json.Marshal(tpl)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", tpl, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", tpl, b, want)
		}
	}
}
Example #10
0
func TestProductMarshal(t *testing.T) {
	expectBytes := []byte(`{
		"product_base":{
			"name":"testaddproduct",
			"category_id":[
				"537074298"
			],
			"main_img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0",
			"img":[
				"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0"
			],
			"detail":[
				{
					"text":"test first"
				},
				{
					"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ul1UcLcwxrFdwTKYhH9Q5YZoCfX4Ncx655ZK6ibnlibCCErbKQtReySaVA/0"
				},
				{
					"text":"test again"
				}
			],
			"property":[
				{
					"id":"1075741879",
					"vid":"1079749967"
				},
				{
					"id":"1075754127",
					"vid":"1079795198"
				},
				{
					"id":"1075777334",
					"vid":"1079837440"
				}
			],
			"sku_info":[
				{
					"id":"1075741873",
					"vid":[
						"1079742386",
						"1079742363"
					]
				}
			],
			"buy_limit":10
		},
		"sku_list":[
			{
				"sku_id":"1075741873:1079742386",
				"price":30,
				"icon_url":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5KWq1QGP3fo6TOTSYD3TBQjuw/0",
				"product_code":"testing",
				"ori_price":9000000,
				"quantity":800
			},
			{
				"sku_id":"1075741873:1079742363",
				"price":30,
				"icon_url":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5KWq1QGP3fo6TOTSYD3TBQjuw/0",
				"product_code":"testingtesting",
				"ori_price":9000000,
				"quantity":800
			}
		],
		"attrext":{
			"location":{
				"country":"中国",
				"province":"广东省",
				"city":"广州市",
				"address":"T.I.T创意园"
			},
			"isPostFree":0,
			"isHasReceipt":1,
			"isUnderGuaranty":0,
			"isSupportReplace":0
		},
		"delivery_info":{
			"delivery_type":0,
			"express":[
				{
					"id":10000027,
					"price":100
				},
				{
					"id":10000028,
					"price":100
				},
				{
					"id":10000029,
					"price":100
				}
			]
		}
	}`)

	_product := Product{
		AttrBase: AttrBase{
			Name:        "testaddproduct",
			CategoryIds: []string{"537074298"},
			MainImage:   "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0",
			Images:      []string{"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0"},
			Details:     []Detail{{Text: "test first"}, {Image: "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ul1UcLcwxrFdwTKYhH9Q5YZoCfX4Ncx655ZK6ibnlibCCErbKQtReySaVA/0"}, {Text: "test again"}},
			Properties:  []Property{{"1075741879", "1079749967"}, {"1075754127", "1079795198"}, {"1075777334", "1079837440"}},
			SKUInfo:     []SKU{{Id: "1075741873", ValueIds: []string{"1079742386", "1079742363"}}},
			BuyLimit:    10,
		},
	}
	_product.AttrExt = &AttrExt{
		Location: Location{
			Country:  "中国",
			Province: "广东省",
			City:     "广州市",
			Address:  "T.I.T创意园",
		},
		IsPostFree:       0,
		IsHasReceipt:     1,
		IsUnderGuaranty:  0,
		IsSupportReplace: 0,
	}
	_product.SKUList = []ProductSKU{
		{
			Id:            "1075741873:1079742386",
			Price:         30,
			IconURL:       "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5KWq1QGP3fo6TOTSYD3TBQjuw/0",
			ProductCode:   "testing",
			OriginalPrice: 9000000,
			Quantity:      800,
		},
		{
			Id:            "1075741873:1079742363",
			Price:         30,
			IconURL:       "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5KWq1QGP3fo6TOTSYD3TBQjuw/0",
			ProductCode:   "testingtesting",
			OriginalPrice: 9000000,
			Quantity:      800,
		},
	}
	_product.DeliveryInfo = NewDeliveryInfoWithExpresses([]Express{
		{
			Id:    10000027,
			Price: 100,
		},
		{
			Id:    10000028,
			Price: 100,
		},
		{
			Id:    10000029,
			Price: 100,
		},
	})

	b, err := json.Marshal(_product)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", _product, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", _product, b, want)
		}
	}
}
Example #11
0
func TestMarshalAndInitFunc(t *testing.T) {
	var expectBytes []byte

	// 控件1 ====================================================================

	expectBytes = []byte(`{
		"eid":1,
    	"group_info":{
        	"group_id":50,
        	"filter":{
            	"count":4
        	}
    	}
	}`)

	var md1 Module
	md1.InitToModule1(50, 4)

	b, err := json.Marshal(md1)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", md1, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", md1, b, want)
		}
	}

	// 控件2 ====================================================================

	expectBytes = []byte(`{
		"eid":2,
		"group_infos":{
			"groups":[
				{
					"group_id":49
				}, 
				{
					"group_id":50
				}, 
				{
					"group_id":51
				}
			]
		}
	}`)

	var md2 Module
	md2.InitToModule2([]int64{49, 50, 51})

	b, err = json.Marshal(md2)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", md2, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", md2, b, want)
		}
	}

	// 控件3 ====================================================================

	expectBytes = []byte(`{
		"eid":3,
		"group_info":{
			"group_id":52, 
			"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5Jm64z4I0TTicv0TjN7Vl9bykUUibYKIOjicAwIt6Oy0Y6a1Rjp5Tos8tg/0"
		}
	}`)

	var md3 Module
	md3.InitToModule3(52, "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5Jm64z4I0TTicv0TjN7Vl9bykUUibYKIOjicAwIt6Oy0Y6a1Rjp5Tos8tg/0")

	b, err = json.Marshal(md3)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", md3, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", md3, b, want)
		}
	}

	// 控件4 ====================================================================

	expectBytes = []byte(`{
		"eid":4,
		"group_infos":{
			"groups":[
				{
					"group_id":49, 
					"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
				}, 
				{
					"group_id":50, 
					"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5G1kdy3ViblHrR54gbCmbiaMnl5HpLGm5JFeENyO9FEZAy6mPypEpLibLA/0"
				}, 
				{
					"group_id":52, 
					"img":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
				}
			]
		}
	}`)

	var md4 Module
	md4.InitToModule4([]Group{
		{
			49,
			"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0",
		},
		{
			50,
			"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5G1kdy3ViblHrR54gbCmbiaMnl5HpLGm5JFeENyO9FEZAy6mPypEpLibLA/0",
		},
		{
			52,
			"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0",
		},
	})

	b, err = json.Marshal(md4)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", md4, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", md4, b, want)
		}
	}

	// 控件5 ====================================================================

	expectBytes = []byte(`{
		"eid":5,
		"group_infos":{
			"groups":[
				{
					"group_id":43
				}, 
				{
					"group_id":44
				}, 
				{
					"group_id":45
				}, 
				{
					"group_id":46
				}
			], 
			"img_background":"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
		}
	}`)

	var md5 Module
	md5.InitToModule5(
		[]int64{43, 44, 45, 46},
		"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0",
	)

	b, err = json.Marshal(md5)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", md5, err)
	} else {
		want := util.TrimSpace(expectBytes)
		if !bytes.Equal(b, want) {
			t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", md5, b, want)
		}
	}
}
Example #12
0
func TestMarshalAndNewFunc(t *testing.T) {

	// 纯文本模板消息==============================================================

	want := util.TrimSpace([]byte(`{
	    "touser":"******",
	    "template_id":"aygtGTLdrjHJP7Bu4EdkptNfYaeFKi98ygn2kitCJ6fAfdmN88naVvX6V5uIV5x0",
	    "data":{
	        "Goods":"苹果",
	        "Unit_price":"RMB 20.13",
	        "Quantity":"5",
	        "Total":"RMB 100.65",
	        "Source":{
	            "Shop":"Jas屌丝商店",
	            "Recommend":"5颗星"
	        }
	    }
	}`))

	var data struct {
		Goods      string
		Unit_price string
		Quantity   string
		Total      string
		Source     struct {
			Shop      string
			Recommend string
		}
	}
	data.Goods = "苹果"
	data.Unit_price = "RMB 20.13"
	data.Quantity = "5"
	data.Total = "RMB 100.65"
	data.Source.Shop = "Jas屌丝商店"
	data.Source.Recommend = "5颗星"

	msg := NewMsg("OPENID", "aygtGTLdrjHJP7Bu4EdkptNfYaeFKi98ygn2kitCJ6fAfdmN88naVvX6V5uIV5x0", &data)

	have, err := json.Marshal(msg)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", msg, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", msg, have, want)
	}

	// 带链接的模板消息==============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":"******",
	    "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
	    "data":{
	        "User":{
	            "value":"黄先生",
	            "color":"#173177"
	        },
	        "Date":{
	            "value":"06月07日 19时24分",
	            "color":"#173177"
	        },
	        "CardNumber":{
	            "value":"0426",
	            "color":"#173177"
	        },
	        "Type":{
	            "value":"消费",
	            "color":"#173177"
	        },
	        "Money":{
	            "value":"人民币260.00元",
	            "color":"#173177"
	        },
	        "DeadTime":{
	            "value":"06月07日19时24分",
	            "color":"#173177"
	        },
	        "Left":{
	            "value":"6504.09",
	            "color":"#173177"
	        }
	    },
		"url":"http://weixin.qq.com/download",
	    "topcolor":"#FF0000"
	}`))

	type DateValue struct {
		Value string `json:"value"`
		Color string `json:"color"`
	}
	var data1 struct {
		User       DateValue
		Date       DateValue
		CardNumber DateValue
		Type       DateValue
		Money      DateValue
		DeadTime   DateValue
		Left       DateValue
	}
	data1.User.Value = "黄先生"
	data1.User.Color = "#173177"
	data1.Date.Value = "06月07日 19时24分"
	data1.Date.Color = "#173177"
	data1.CardNumber.Value = "0426"
	data1.CardNumber.Color = "#173177"
	data1.Type.Value = "消费"
	data1.Type.Color = "#173177"
	data1.Money.Value = "人民币260.00元"
	data1.Money.Color = "#173177"
	data1.DeadTime.Value = "06月07日19时24分"
	data1.DeadTime.Color = "#173177"
	data1.Left.Value = "6504.09"
	data1.Left.Color = "#173177"

	msg1 := NewMsgWithLink("OPENID", "ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
		&data1, "http://weixin.qq.com/download", "#FF0000")

	have, err = json.Marshal(msg1)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", msg1, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", msg1, have, want)
	}
}
Example #13
0
func TestMarshalAndNewFunc(t *testing.T) {

	// 测试文本消息===============================================================

	want := util.TrimSpace([]byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>text</MsgType>
		<Content>你好</Content>
	</xml>`))

	text := NewText("toUser", "fromUser", "你好", 12345678)

	have, err := xml.Marshal(text)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", text, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", text, have, want)
	}

	// 测试图片消息===============================================================

	want = util.TrimSpace([]byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>image</MsgType>
		<Image>
			<MediaId>media_id</MediaId>
		</Image>
	</xml>`))

	image := NewImage("toUser", "fromUser", "media_id", 12345678)

	have, err = xml.Marshal(image)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", image, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", image, have, want)
	}

	// 测试语音消息===============================================================

	want = util.TrimSpace([]byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>voice</MsgType>
		<Voice>
			<MediaId>media_id</MediaId>
		</Voice>
	</xml>`))

	voice := NewVoice("toUser", "fromUser", "media_id", 12345678)

	have, err = xml.Marshal(voice)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", voice, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", voice, have, want)
	}

	// 测试视频消息===============================================================

	want = util.TrimSpace([]byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>video</MsgType>
		<Video>
			<MediaId>media_id</MediaId>
			<Title>title</Title>
			<Description>description</Description>
		</Video> 
	</xml>`))

	video := NewVideo("toUser", "fromUser", "media_id", "title", "description", 12345678)

	have, err = xml.Marshal(video)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", video, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", video, have, want)
	}

	// 测试音乐消息===============================================================

	want = util.TrimSpace([]byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>music</MsgType>
		<Music>
			<Title>TITLE</Title>
			<Description>DESCRIPTION</Description>
			<MusicUrl>MUSIC_Url</MusicUrl>
			<HQMusicUrl>HQ_MUSIC_Url</HQMusicUrl>
			<ThumbMediaId>media_id</ThumbMediaId>
		</Music>
	</xml>`))

	music := NewMusic("toUser", "fromUser", "media_id",
		"MUSIC_Url", "HQ_MUSIC_Url", "TITLE", "DESCRIPTION", 12345678)

	have, err = xml.Marshal(music)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", music, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", music, have, want)
	}

	// 测试图文消息, 没有文章=======================================================

	want = util.TrimSpace([]byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>news</MsgType>
		<ArticleCount>0</ArticleCount>
		<Articles>
		</Articles>
	</xml>`))

	news := NewNews("toUser", "fromUser", make([]NewsArticle, 0, 2), 12345678)

	have, err = xml.Marshal(news)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", news, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", news, have, want)
	}

	// 测试图文消息, 1篇文章=======================================================

	want = util.TrimSpace([]byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>news</MsgType>
		<ArticleCount>1</ArticleCount>
		<Articles>
			<item>
				<Title>title1</Title> 
				<Description>description1</Description>
				<PicUrl>picurl</PicUrl>
				<Url>url</Url>
			</item>
		</Articles>
	</xml>`))

	news.AppendArticle(NewsArticle{
		Title:       "title1",
		Description: "description1",
		PicURL:      "picurl",
		URL:         "url",
	})

	have, err = xml.Marshal(news)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", news, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", news, have, want)
	}

	// 测试图文消息, 2篇文章=======================================================

	want = util.TrimSpace([]byte(`<xml>
		<ToUserName>toUser</ToUserName>
		<FromUserName>fromUser</FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType>news</MsgType>
		<ArticleCount>2</ArticleCount>
		<Articles>
			<item>
				<Title>title1</Title> 
				<Description>description1</Description>
				<PicUrl>picurl</PicUrl>
				<Url>url</Url>
			</item>
			<item>
				<Title>title</Title>
				<Description>description</Description>
				<PicUrl>picurl</PicUrl>
				<Url>url</Url>
			</item>
		</Articles>
	</xml>`))

	news.AppendArticle(NewsArticle{
		Title:       "title",
		Description: "description",
		PicURL:      "picurl",
		URL:         "url",
	})

	have, err = xml.Marshal(news)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", news, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", news, have, want)
	}

	// 测试将消息转发到多客服=======================================================

	want = util.TrimSpace([]byte(`<xml>
		<ToUserName>touser</ToUserName>
		<FromUserName>fromuser</FromUserName>
		<CreateTime>1399197672</CreateTime>
		<MsgType>transfer_customer_service</MsgType>
	</xml>`))

	transToCS := NewTransferToCustomerService("touser", "fromuser", 1399197672)

	have, err = xml.Marshal(transToCS)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", transToCS, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", transToCS, have, want)
	}

	// 测试将消息转发到指定客服=======================================================

	want = util.TrimSpace([]byte(`<xml> 
		<ToUserName>touser</ToUserName>  
		<FromUserName>fromuser</FromUserName>  
		<CreateTime>1399197672</CreateTime>  
		<MsgType>transfer_customer_service</MsgType>  
		<TransInfo> 
			<KfAccount>test1@test</KfAccount> 
		</TransInfo> 
	</xml>`))

	transToSCS := NewTransferToSpecialCustomerService("touser", "fromuser", "test1@test", 1399197672)

	have, err = xml.Marshal(transToSCS)
	if err != nil {
		t.Errorf("xml.Marshal(%#q):\nError: %s\n", transToSCS, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("xml.Marshal(%#q):\nhave %#s\nwant %#s\n", transToSCS, have, want)
	}
}
Example #14
0
func TestMarshalAndNewFunc(t *testing.T) {

	// 测试文本消息===============================================================

	want := util.TrimSpace([]byte(`{
	    "touser":"******", 
	    "msgtype":"text", 
	    "text":{
	        "content":"Hello World"
	    }
	}`))

	text := NewText("OPENID", "Hello World")

	have, err := json.Marshal(text)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", text, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", text, have, want)
	}

	// 测试图片消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":"******", 
	    "msgtype":"image", 
	    "image":{
	        "media_id":"MEDIA_ID"
	    }
	}`))

	image := NewImage("OPENID", "MEDIA_ID")

	have, err = json.Marshal(image)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", image, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", image, have, want)
	}

	// 测试语音消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":"******", 
	    "msgtype":"voice", 
	    "voice":{
	        "media_id":"MEDIA_ID"
	    }
	}`))

	voice := NewVoice("OPENID", "MEDIA_ID")

	have, err = json.Marshal(voice)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", voice, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", voice, have, want)
	}

	// 测试视频消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":"******", 
	    "msgtype":"video", 
	    "video":{
	        "media_id":"MEDIA_ID", 
	        "thumb_media_id":"THUMB_MEDIA_ID", 
	        "title":"TITLE", 
	        "description":"DESCRIPTION"
	    }
	}`))

	video := NewVideo("OPENID", "MEDIA_ID", "THUMB_MEDIA_ID", "TITLE", "DESCRIPTION")

	have, err = json.Marshal(video)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", video, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", video, have, want)
	}

	// 测试音乐消息===============================================================

	want = util.TrimSpace([]byte(`{
	    "touser":"******", 
	    "msgtype":"music", 
	    "music":{
	        "title":"MUSIC_TITLE", 
	        "description":"MUSIC_DESCRIPTION", 
	        "musicurl":"MUSIC_URL", 
	        "hqmusicurl":"HQ_MUSIC_URL", 
	        "thumb_media_id":"THUMB_MEDIA_ID"
	    }
	}`))

	music := NewMusic("OPENID", "THUMB_MEDIA_ID", "MUSIC_URL", "HQ_MUSIC_URL", "MUSIC_TITLE", "MUSIC_DESCRIPTION")

	have, err = json.Marshal(music)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", music, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", music, have, want)
	}

	// 图文消息==================================================================

	articles := make([]NewsArticle, 2)
	articles[0].Init("Happy Day1", "Is Really A Happy Day1", "URL1", "PIC_URL1")
	articles[1].Init("Happy Day2", "Is Really A Happy Day2", "URL2", "PIC_URL2")

	// 测试图文消息, 没有文章=======================================================

	want = util.TrimSpace([]byte(`{
	    "touser":"******", 
	    "msgtype":"news", 
	    "news":{}
	}`))

	news := NewNews("OPENID", articles[:0])

	have, err = json.Marshal(news)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", news, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", news, have, want)
	}

	// 测试图文消息, 1篇文章=======================================================

	want = util.TrimSpace([]byte(`{
	    "touser":"******", 
	    "msgtype":"news", 
	    "news":{
	        "articles":[
	            {
	                "title":"Happy Day1", 
	                "description":"Is Really A Happy Day1", 
	                "url":"URL1", 
	                "picurl":"PIC_URL1"
	            }
	        ]
	    }
	}`))

	news = NewNews("OPENID", articles[:1])

	have, err = json.Marshal(news)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", news, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", news, have, want)
	}

	// 测试图文消息, 2篇文章=======================================================

	want = util.TrimSpace([]byte(`{
	    "touser":"******", 
	    "msgtype":"news", 
	    "news":{
	        "articles":[
	            {
	                "title":"Happy Day1", 
	                "description":"Is Really A Happy Day1", 
	                "url":"URL1", 
	                "picurl":"PIC_URL1"
	            }, 
	            {
	                "title":"Happy Day2", 
	                "description":"Is Really A Happy Day2", 
	                "url":"URL2", 
	                "picurl":"PIC_URL2"
	            }
	        ]
	    }
	}`))

	news = NewNews("OPENID", articles[:2])

	have, err = json.Marshal(news)
	if err != nil {
		t.Errorf("json.Marshal(%#q):\nError: %s\n", news, err)
	} else if !bytes.Equal(have, want) {
		t.Errorf("json.Marshal(%#q):\nhave %#s\nwant %#s\n", news, have, want)
	}
}