• 测试网站接口,nginx篇


    nginx是反向代理,怎么通过nginx反向代理要测试接口的线上网站呢。

    这里自我提供了一个方法,仅供参考!建议不要用于刷接口等非常规的用途,后果会很严重。

    首先 用node express创建一个项目test,然后安装依赖什么的等等,不再多说,可搜索express创建项目,我这里新建一个ejs项目,页面包含接口测试,以及日志处理输出等,项目服务器地址是127.0.0.1:7171。

    <!DOCTYPE html>
    <html>
      <head>
        <title><%= title %></title>
        <link rel='stylesheet' href='/test/stylesheets/style.css' />
        <script src='/test/javascripts/jquery-1.8.3.min.js'></script>
      </head>
      <body>
        <h1><%= title %></h1>
        <button id="testBtn">测试接口</button><span id='testResult'></span>
        <p id='timeLeft' style="color:red;">倒计时:0</p>
        <div style=" 820px;">
          <div id='content1' style='400px;height: 400px;overflow-y: auto;border:1px solid #00f;display: inline-block;word-break: break-all;word-wrap: break-word;'>积分日志信息</div>
          <div id='content2' style='400px;height: 400px;overflow-y: auto;border:1px solid #00f;display: inline-block;word-break: break-all;word-wrap: break-word;'>正常日志信息</div>
          <div style=' 400px;display: inline-block;'>
            <button id='startBtn1' type='button' value='开始'>开始</button>
            <button id='throwBtn1' type='button' value='投掷'>接口1</button>
            <span id='totalNum'>0</span>
          </div>
          <div style=" 400px;display: inline-block;">
            <button id='startBtn2' type='button' value='开始'>开始</button>
            <button id='throwBtn2' type='button' value='投掷'>接口2</button>
          </div>
        </div>
        <script src='/test/javascripts/accessAll.js'></script>
      </body>
    </html>

    显示效果

    注意这里导入了jquery主要是为了接口通讯方便,accessAll是接口处理,在这里可以为所欲为,可以对接口请求数据进行更改和对返回数据分析,贴一个简单的测试代码供参考:

    //测试接口
    $('#testBtn').click(function(){
        $.ajax({
            "url":'接口url(注意这里不能包含网站域名,否则跨域)',
            "type": "GET",
            success:function(res){
                if(res.result){
                    $('#testResult').text('接口正常');
                }else{
                    $('#testResult').text(res.msg);
                }
            },
            complete:function(){
                $('#testResult').text = "接口异常";
            },
            fail:function(){
                $('#testResult').text = "http error";
            }
        });
    });

    接下来最重要的就是怎么让这个网页能正常请求到信息而不跨域呢。

    先配置一个nginx服务器地址,端口1111,

    接下来,假设要测试接口的网站为test.net,我们把其根代理指向test.net,这时启动nginx,在浏览器输入127.0.0.1:1111试下是不是直接跳到

    test,net了,第一步完成。

    第二步 配置自己的测试页nginx配置,proxy路径为/test/。到这里基本就玩成配置了,重启nginx,在浏览器启动两个网页,一个是根网页127.0.0.1:1111,另一个是自己的测试页127.0.0.1:1111/test/,这时就可以通过test页正常请求test.net的接口了,注意接口路径。

    完整nginx配置

    server {
            listen       1111;
            server_name  localhost;
    
            location / {
            proxy_pass    https://test.net/;
            }
    
        location /test/ {
            proxy_pass   https://127.0.0.1:1717/;
            }
        
            
            error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                root   html;
            }
        }

    简单的接口测试,当然还有其他很多方法,我这个只是自己摸索出来的。

  • 相关阅读:
    excel导入phpmyadmin
    Python 教程资源
    SAE部署Python-让云端自动运行Python代码
    python socket 超时设置 errno10054
    使用python一步一步搭建微信公众平台(一)
    PHP读取MySQL数据
    php连接mysql并读取数据
    php分页例子实现读取mysql数据分页显示
    51Nod1305 Pairwise Sum and Divide
    SPOJ SUBXOR
  • 原文地址:https://www.cnblogs.com/wangzisheng/p/9075483.html
Copyright © 2020-2023  润新知