You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
15 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#region Apache License Version 2.0
/*----------------------------------------------------------------
Copyright 2023 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.
Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
----------------------------------------------------------------*/
#endregion Apache License Version 2.0
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.GroupMessage;
using Senparc.Weixin.MP.AdvancedAPIs.Media;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Test.CommonAPIs;
using Senparc.CO2NET.Extensions;
namespace Senparc.Weixin.MP.Test.AdvancedAPIs
{
//已测试通过
[TestClass]
public class MediaTest : CommonApiTest
{
private string mediaId = null;
[TestMethod]
public void UploadTemporaryMediaTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var type = UploadMediaFileType.image;
#if NETCOREAPP2_0_OR_GREATER || NET6_0_OR_GREATER
var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\qr.jpg");
#else
var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\qr.jpg");
#endif
var result = MediaApi.UploadTemporaryMedia(accessToken, type, file);
Assert.AreEqual(type, result.type);
Assert.IsNotNull(result.media_id);
mediaId = result.media_id;
}
[TestMethod]
public void UploadTemporaryNewsTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var file = @"E:\1.jpg";
var mediaId = MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.thumb, file).thumb_media_id;
Assert.IsNotNull(mediaId);
var new1 = new NewsModel()
{
author = "test",
content = "test",
content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
digest = "test",
show_cover_pic = "1",
thumb_media_id = mediaId,
title = "test"
};
var new2 = new NewsModel()
{
author = "test",
content = "test111",
content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
digest = "test",
show_cover_pic = "1",
thumb_media_id = mediaId,
title = "test"
};
var result = MediaApi.UploadTemporaryNews(accessToken, 10000, new1, new2);
Assert.IsNotNull(result.media_id);
}
[TestMethod]
public void GetStreamTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
UploadTemporaryMediaTest();//上传
using (MemoryStream ms = new MemoryStream())
{
MediaApi.Get(accessToken, mediaId, ms);
Assert.IsTrue(ms.Length > 0);
//保存到文件
#if NETCOREAPP2_0_OR_GREATER || NET6_0_OR_GREATER
var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\testpic_{0}.core20.jpg".FormatWith(SystemTime.Now.ToString("yyyyMMddHHmmss")));
#else
var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\testpic_{0}.net45.jpg".FormatWith(SystemTime.Now.ToString("yyyyMMddHHmmss")));
#endif
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
ms.Position = 0;
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
fs.Flush();
}
Assert.IsTrue(File.Exists(fileName));
}
}
[TestMethod]
public void GetDirTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
UploadTemporaryMediaTest();//上传
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP2_1_OR_GREATER || NET6_0_OR_GREATER
var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\");
#else
var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\");
#endif
var fileName = MediaApi.Get(accessToken, mediaId, dir);
Assert.IsTrue(File.Exists(fileName));
Console.WriteLine("原始文件:" + fileName);
}
[TestMethod()]
public void GetVoiceTest()
{
string serverId = "IT41QWoGSnkt5fj01mK2ByhgRACBgvRW6fGP3bt9QAjH8vwqsra9qYJkj8LCXzNS";
var file = SystemTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ".speex"; //ServerUtility.ContentRootMapPath("~/../")
using (MemoryStream ms = new MemoryStream())
{
MediaApi.Get(base._appId, serverId, ms);
//保存到文件
ms.Position = 0;
byte[] buffer = new byte[1024];
int bytesRead = 0;
//判断是否上传成功
byte[] topBuffer = new byte[1];
ms.Read(topBuffer, 0, 1);
if (topBuffer[0] == '{')
{
//写入日志
ms.Position = 0;
byte[] logBuffer = new byte[1024];
ms.Read(logBuffer, 0, logBuffer.Length);
string str = System.Text.Encoding.Default.GetString(logBuffer);
Console.WriteLine(str);
Assert.Fail();
}
else
{
ms.Position = 0;
//创建目录
using (FileStream fs = new FileStream(file, FileMode.Create))
{
while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
fs.Flush();
}
}
}
}
private string UploadForeverMediaTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var file = GetParentRootRelativePath() + @"AdvancedAPIs\Media\test.jpg";
var result = MediaApi.UploadForeverMediaAsync(accessToken, file, UploadForeverMediaType.image).GetAwaiter().GetResult();
Assert.IsNotNull(result.media_id);
Console.WriteLine("上传永久素材成功mediaId" + result.media_id);
mediaId = result.media_id;
return mediaId;
}
[TestMethod]
public void UploadForeverVideoTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var file = @"E:\Test.mp4";
var result = MediaApi.UploadForeverVideo(accessToken, file, "测试", "测试", UploadForeverMediaType.video);
Assert.IsNotNull(result.media_id);
mediaId = result.media_id;
}
//[TestMethod]
private string UploadAndUpdateNewsTest(string accessToken)
{
//var file = @"E:\1.jpg";
//var result = MediaApi.UploadForeverMedia(accessToken, file, UploadForeverMediaType.image);
//Assert.IsNotNull(result.media_id);
var mediaId = UploadForeverMediaTest();
var new1 = new NewsModel()
{
author = "test",
content = "test",
content_source_url = "https://weixin.senparc.com/Content/Images/2019/logo-community-two-lines.png",
digest = "test",
show_cover_pic = "1",
thumb_media_id = mediaId,
title = "test"
};
var new2 = new NewsModel()
{
author = "test",
content = "test111",
content_source_url = "https://weixin.senparc.com/Content/Images/2019/logo-community-two-lines.png",
digest = "test",
show_cover_pic = "1",
thumb_media_id = mediaId,
title = "test"
};
var result = MediaApi.UploadNewsAsync(accessToken, 10000, new1, new2).GetAwaiter().GetResult();
Assert.IsNotNull(result.media_id);
Console.WriteLine("上传图文素材成功mediaId" + result.media_id);
//var result2 = MediaApi.UpdateForeverNews(accessToken, result1.media_id, 0, 10000, new2);
//Assert.AreEqual(result2.errcode, ReturnCode.请求成功);
return result.media_id;
}
[TestMethod]
public void GetForeverMediaTest()
{
var mediaId = UploadForeverMediaTest();
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
using (MemoryStream stream = new MemoryStream())
{
MediaApi.GetForeverMedia(accessToken, mediaId, stream);
Assert.IsTrue(stream.Length > 0);
var fileName = GetParentRootRelativePath() + @"AdvancedAPIs\Media\test.download." + SystemTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".jpg";
using (var fs = new FileStream(fileName, FileMode.CreateNew))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fs);
fs.Flush();
}
}
}
//[TestMethod]
private void GetForeverNewsTest(string accessToken, string mediaId)
{
var result = MediaApi.GetForeverNews(accessToken, mediaId);
Assert.IsTrue(result.news_item.Count > 0);
Assert.AreEqual(result.news_item[0].content, "test");
Console.WriteLine($"获取图文素材成功,文章数量:{result.news_item.Count}");
Console.WriteLine("图文信息:");
Console.WriteLine(result.news_item.ToJson(true));
}
//[TestMethod]
private void DeleteForeverMediaTest(string accessToken, string mediaId)
{
var result = MediaApi.DeleteForeverMedia(accessToken, mediaId);
Assert.AreEqual(result.errcode, ReturnCode.);
Console.WriteLine("图文素材已删除");
}
[TestMethod]
public void ForeverNewsTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
string mediaId = UploadAndUpdateNewsTest(accessToken);
GetForeverNewsTest(accessToken, mediaId);
DeleteForeverMediaTest(accessToken, mediaId);
}
[TestMethod]
public void GetMediaListTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var result = MediaApi.GetNewsMediaList(accessToken, 0, 5);
Assert.IsNotNull(result.item_count);
}
[TestMethod]
public void GetNewsMediaListTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var result = MediaApi.GetNewsMediaList(accessToken, 0, 3);
Assert.AreEqual(result.errcode, ReturnCode.);
Assert.AreEqual(result.item_count, 3);
}
[TestMethod]
public void GetOthersMediaListTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var result = MediaApi.GetOthersMediaList(accessToken, UploadMediaFileType.image, 0, 3);
Assert.AreEqual(result.errcode, ReturnCode.);
Assert.AreEqual(result.item_count, 3);
}
[TestMethod]
public void AfterDeleteImgTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var file = @"..\..\AdvancedAPIs\Media\test.jpg";
var result = MediaApi.UploadForeverMedia(accessToken, file, UploadForeverMediaType.image);
Assert.IsNotNull(result.media_id);
CustomApi.SendImage(accessToken, "o3IHxjrPzMVZIJOgYMH1PyoTW_Tg", result.media_id);
MediaApi.DeleteForeverMedia(accessToken, result.media_id);
}
[TestMethod]
public void AfterDeleteNewsTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var file = @"E:\1.jpg";
var result = MediaApi.UploadForeverMedia(accessToken, file, UploadForeverMediaType.image);
Assert.IsNotNull(result.media_id);
var new1 = new NewsModel()
{
author = "test",
content = "test",
content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
digest = "test",
show_cover_pic = "1",
thumb_media_id = result.media_id,
title = "test"
};
var new2 = new NewsModel()
{
author = "test",
content = "test111",
content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
digest = "test",
show_cover_pic = "1",
thumb_media_id = result.media_id,
title = "test"
};
var result1 = MediaApi.UploadNews(accessToken, 10000, new1, new2);
Assert.IsNotNull(result1.media_id);
var clientMsgId = SystemTime.Now.Ticks.ToString();
GroupMessageApi.SendGroupMessageByOpenId(accessToken, GroupMessageType.mpnews, result1.media_id, clientMsgId, 10000, "o3IHxjrPzMVZIJOgYMH1PyoTW_Tg", "o3IHxjrPzMVZIJOgYMH1PyoTW_Tg");
//var result2 = MediaApi.UpdateForeverNews(accessToken, result1.media_id, 0, 10000, new2);
MediaApi.DeleteForeverMedia(accessToken, result1.media_id);
//Assert.AreEqual(result2.errcode, ReturnCode.请求成功);
}
[TestMethod]
public void AfterDeleteVideoTest()
{
var accessToken = AccessTokenContainer.GetAccessToken(_appId);
var file = @"E:\Test.mp4";
var result = MediaApi.UploadForeverVideo(accessToken, file, "测试", "测试", UploadForeverMediaType.video, 100000);
Assert.IsNotNull(result.media_id);
CustomApi.SendVideo(accessToken, "o3IHxjrPzMVZIJOgYMH1PyoTW_Tg", result.media_id, "测试", "测试");
MediaApi.DeleteForeverMedia(accessToken, result.media_id);
}
}
}