如何测试apiController [英] how to test apiController

查看:339
本文介绍了如何测试apiController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VS 2013中处理现有的Windows Service项目.

Im working on an existing Windows Service project in VS 2013.

我添加了一个Web API控制器类,我现在不记得它是(v2.1)还是(v1)控制器类....无论如何,我都将其称为SynchroniseFromAwsService

I've added a web API Controller class I cant remember now if its a (v2.1) or (v1) controller class....Anyway I've called it SynchroniseFromAwsService

我正在尝试从AWS lambda调用中调用它,但它告诉我我没有访问权限.因此,我需要在本地对其进行测试,以查看它是否可以尝试诊断问题.

Im trying to call it from a AWS lambda call but it is telling me I dont have access. So I need to test it locally to see if it is working to try and diagnose the issue.

我想在本地进行测试,但不确定如何进行测试..请参阅代码...

I want to test this locally but am unsure how to do so..please see code...

 namespace Workflow
{
    public class SynchroniseFromAwsService: ApiController
    {
        //// POST api/<controller>
        public string SapCall([FromBody]string xmlFile)
        {
            string responseMsg = "Failed Import User";

            if (!IsNewestVersionOfXMLFile(xmlFile))
            {
                responseMsg = "Not latest version of file, update not performed";
            }
            else
            {
                Business.PersonnelReplicate personnelReplicate = BusinessLogic.SynchronisePersonnel.BuildFromDataContractXml<Business.PersonnelReplicate>(xmlFile);
                bool result = Service.Personnel.SynchroniseCache(personnelReplicate);

                if (result)
                {
                    responseMsg = "Success Import Sap Cache User";
                }
            }

            return "{\"response\" : \" " + responseMsg + " \" , \"isNewActiveDirectoryUser\" : \" false \"}";
        }
}
}

我已经在Google上阅读过以下载名为Postman的程序并对其进行测试.

I've read on google to download a program called Postman and test it.

在VS中没有什么我可以调用的东西,只是传入一个包含xml文件的虚拟数据字符串进行测试吗?

Is there nothing I can call in VS and just pass in a dummy data string containing an xml file to test?

什么是最好的方法

感谢您的答复

推荐答案

您的代码正确.您需要在本地对其进行测试.您可以使用Postman rest客户本身来完成它.非常简单.
在您的代码中,函数 SapCall 缺少 HTTP 方法属性.因此,请为用于标识此请求类型的方法指定属性.

Your code is right. You need to test it locally is it. You can just accomplish it using Postman rest client itself. It's very simple.
In your code the HTTP method attribute is missing for your function SapCall. So, specify attribute for your method used to identify which type of request is this.

[HttpPost]
public string SapCall([FromBody]string xmlFile){

现在使用邮递员休息客户端并调用您的API网址.它将成功执行.

Now use postman rest client and call ur API url. It will get execute successfully.

这篇关于如何测试apiController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆