Пример #1
0
        public MessageMapperTest()
        {
            var wechatClient = MockDataUtility.GetMockWeChatClient();

            this.wechatMessageMapper  = new WeChatMessageMapper(wechatClient, true);
            this.wechatMessageMapper2 = new WeChatMessageMapper(wechatClient, false);
        }
Пример #2
0
        public async Task ToWeChatMessagesTest_EventActivity()
        {
            var activityList = MockDataUtility.GetMockEventActivityList();

            foreach (var activity in activityList)
            {
                var wechatResponses = await wechatMessageMapper.ToWeChatMessages(activity);
            }
        }
Пример #3
0
        public async Task ToConnectorMessageTest_TestRequest()
        {
            var mockRequestList = MockDataUtility.GetMockRequestMessageList();

            foreach (var mockRequest in mockRequestList)
            {
                var activity = await wechatMessageMapper.ToConnectorMessage(mockRequest);

                AssertGeneralParameters(mockRequest, activity);
            }
        }
Пример #4
0
        public async Task ToWeChatMessagesTest_MessageActivity()
        {
            var activityList = MockDataUtility.GetMockMessageActivityList();

            foreach (var messageActivity in activityList)
            {
                var wechatResponses = await wechatMessageMapper.ToWeChatMessages(messageActivity);

                var wechatResponses2 = await wechatMessageMapper2.ToWeChatMessages(messageActivity);

                Assert.True(wechatResponses.Count > 0);
                Assert.True(wechatResponses2.Count > 0);
            }
        }
Пример #5
0
        public async Task ToWeChatMessagesTest_MessageActivityWithAttachment()
        {
            var messageActivity = MockDataUtility.GetMockMessageActivity();
            var attachments     = await MockDataUtility.GetGeneralAttachmentList(true);

            foreach (var att in attachments)
            {
                messageActivity.Attachments.Add(att);
            }

            var wechatResponses = await wechatMessageMapper.ToWeChatMessages(messageActivity);

            var wechatResponses2 = await wechatMessageMapper2.ToWeChatMessages(messageActivity);

            Assert.True(wechatResponses2.Count > 0);
            Assert.True(wechatResponses.Count > 0);
        }
        public async Task UploadMediaTest()
        {
            var storage            = new MemoryStorage();
            var mockClient         = new MockWeChatClient(settings, storage);
            var mockAttachemntData = MockDataUtility.GetMockAttachmentData();
            var result1            = await mockClient.UploadMediaAsync(mockAttachemntData, true, 10000) as UploadTemporaryMediaResult;

            var cachedResult1 = await mockClient.UploadMediaAsync(mockAttachemntData, true, 10000) as UploadTemporaryMediaResult;

            var result3 = await mockClient.UploadNewsAsync(new News[] { new News {
                                                                            Title = "test"
                                                                        } }, true) as UploadTemporaryMediaResult;

            var cachedResult3 = await mockClient.UploadNewsAsync(new News[] { new News {
                                                                                  Title = "test"
                                                                              } }, true) as UploadTemporaryMediaResult;

            var result4 = await mockClient.UploadMediaAsync(mockAttachemntData, false, 10000) as UploadPersistentMediaResult;

            var cachedResult4 = await mockClient.UploadMediaAsync(mockAttachemntData, false, 10000) as UploadPersistentMediaResult;

            var result5 = await mockClient.UploadNewsAsync(new News[] { new News {
                                                                            Title = "test"
                                                                        } }, false) as UploadPersistentMediaResult;

            var cachedResult5 = await mockClient.UploadNewsAsync(new News[] { new News {
                                                                                  Title = "test"
                                                                              } }, false) as UploadPersistentMediaResult;

            var result6 = await mockClient.UploadNewsImageAsync(mockAttachemntData);

            var cachedResult6 = await mockClient.UploadNewsImageAsync(mockAttachemntData);

            Assert.Equal("mediaId", result1.MediaId);
            Assert.Equal("mediaId", cachedResult1.MediaId);
            Assert.Equal(MediaTypes.News, result3.Type);
            Assert.Equal(MediaTypes.News, cachedResult3.Type);
            Assert.Equal("foreverMedia", result4.MediaId);
            Assert.Equal("foreverMedia", cachedResult4.MediaId);
            Assert.Equal("foreverNews", result5.MediaId);
            Assert.Equal("foreverNews", cachedResult5.MediaId);
            Assert.Equal("foreverImage", result6.MediaId);
            Assert.Equal("foreverImage", cachedResult6.MediaId);
        }
        public override Task <byte[]> SendHttpRequestAsync(HttpMethod method, string url, object data = null, string token = null, int timeout = 10000)
        {
            var result     = JsonConvert.SerializeObject(MockDataUtility.WeChatJsonResult);
            var byteResult = Encoding.UTF8.GetBytes(result);

            if (url.Contains("cgi-bin/token"))
            {
                var tokenResult = new AccessTokenResult()
                {
                    ExpireIn = 7200,
                    Token    = "testToken",
                };
                result     = JsonConvert.SerializeObject(tokenResult);
                byteResult = Encoding.UTF8.GetBytes(result);
            }
            else if (url.Contains("upload?access_token"))
            {
                result     = JsonConvert.SerializeObject(MockDataUtility.MockTempMediaResult(MediaTypes.Image));
                byteResult = Encoding.UTF8.GetBytes(result);
            }
            else if (url.Contains("add_material"))
            {
                result     = JsonConvert.SerializeObject(MockDataUtility.MockForeverMediaResult("foreverMedia"));
                byteResult = Encoding.UTF8.GetBytes(result);
            }
            else if (url.Contains("uploadnews"))
            {
                result     = JsonConvert.SerializeObject(MockDataUtility.MockTempMediaResult(MediaTypes.News));
                byteResult = Encoding.UTF8.GetBytes(result);
            }
            else if (url.Contains("add_news"))
            {
                result     = JsonConvert.SerializeObject(MockDataUtility.MockForeverMediaResult("foreverNews"));
                byteResult = Encoding.UTF8.GetBytes(result);
            }
            else if (url.Contains("uploadimg"))
            {
                result     = JsonConvert.SerializeObject(MockDataUtility.MockForeverMediaResult("foreverImage"));
                byteResult = Encoding.UTF8.GetBytes(result);
            }

            return(Task.FromResult(byteResult));
        }
        public async Task WeChatHttpAdapterTest()
        {
            var request      = CreateMockRequest(MockDataUtility.XmlEncrypt).Object;
            var response     = CreateMockResponse().Object;
            var secretInfo   = MockDataUtility.GetMockSecretInfo();
            var storage      = new MemoryStorage();
            var taskQueue    = new BackgroundTaskQueue();
            var bot          = new EchoBot();
            var testAdapter1 = new WeChatHttpAdapter(MockDataUtility.MockWeChatSettings(true, false), storage, taskQueue);
            var testAdapter2 = new WeChatHttpAdapter(MockDataUtility.MockWeChatSettings(false, true), storage, taskQueue);
            var testAdapter3 = new WeChatHttpAdapter(MockDataUtility.MockWeChatSettings(true, true), storage, taskQueue);
            var testAdapter4 = new WeChatHttpAdapter(MockDataUtility.MockWeChatSettings(false, false), storage, taskQueue);

            await testAdapter1.ProcessAsync(request, response, bot, secretInfo);

            await testAdapter2.ProcessAsync(request, response, bot, secretInfo);

            await testAdapter3.ProcessAsync(request, response, bot, secretInfo);

            await testAdapter4.ProcessAsync(request, response, bot, secretInfo);
        }
        public async Task WeChatHttpAdapterExceptionTest()
        {
            var request      = CreateMockRequest(MockDataUtility.XmlEncrypt).Object;
            var response     = CreateMockResponse().Object;
            var secretInfo   = MockDataUtility.GetMockSecretInfo();
            var storage      = new MemoryStorage();
            var taskQueue    = new BackgroundTaskQueue();
            var bot          = new EchoBot();
            var testAdapter  = new WeChatHttpAdapter(MockDataUtility.MockWeChatSettings(true, false), storage, taskQueue);
            var testAdapter2 = new WeChatHttpAdapter(MockDataUtility.MockWeChatSettings(true, false), storage, null);
            var nullQueue    = await Assert.ThrowsAsync <NullReferenceException>(() => testAdapter2.ProcessAsync(request, response, bot, secretInfo)).ConfigureAwait(false);

            Assert.Equal("Background task queue can not be null.", nullQueue.Message);
            testAdapter2.OnTurnError = (context, exception) => { return(Task.CompletedTask); };
            await testAdapter2.ProcessAsync(request, response, bot, secretInfo).ConfigureAwait(false);

            var nullRequest = await Assert.ThrowsAsync <ArgumentNullException>(() => testAdapter.ProcessAsync(null, response, bot, secretInfo)).ConfigureAwait(false);

            Assert.Equal("Value cannot be null.\r\nParameter name: httpRequest", nullRequest.Message);
            var nullResponse = await Assert.ThrowsAsync <ArgumentNullException>(() => testAdapter.ProcessAsync(request, null, bot, secretInfo)).ConfigureAwait(false);

            Assert.Equal("Value cannot be null.\r\nParameter name: httpResponse", nullResponse.Message);
            var nullBot = await Assert.ThrowsAsync <ArgumentNullException>(() => testAdapter.ProcessAsync(request, response, null, secretInfo)).ConfigureAwait(false);

            Assert.Equal("Value cannot be null.\r\nParameter name: bot", nullBot.Message);
            var nullSecretInfo = await Assert.ThrowsAsync <ArgumentNullException>(() => testAdapter.ProcessAsync(request, response, bot, null)).ConfigureAwait(false);

            Assert.Equal("Value cannot be null.\r\nParameter name: secretInfo", nullSecretInfo.Message);

            secretInfo.EchoString = "echoString";
            await testAdapter.ProcessAsync(request, response, bot, secretInfo).ConfigureAwait(false);

            Assert.Equal(response.Body.Length, secretInfo.EchoString.Length);
            secretInfo.Timestamp = "0";
            var unauthorized = await Assert.ThrowsAsync <UnauthorizedAccessException>(() => testAdapter.ProcessAsync(request, response, bot, secretInfo)).ConfigureAwait(false);

            Assert.Equal("Signature verification failed.", unauthorized.Message);

            testAdapter.Dispose();
        }