go http header 重定向测试用例

1  go grpc-go 相关技术专栏 总入口

2  Protobuf介绍与实战 图文专栏 文章目录

本篇文章仅作为笔记使用。

测试重定向

在http.ResponseWriter类型中设置重定向

测试用例1:模拟一下,重定向到某个url地址,如百度


package header

import (
	"fmt"
	"log"
	"net/http"
	"testing"
)

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Print(fmt.Sprintf("%v+", r))
	w.Header().Set("Cache-Control", "must-revalidate, no-store")
	w.Header().Set("Content-Type", " text/html;charset=UTF-8")
	w.Header().Set("Location", "http://www.baidu.com/") //跳转地址设置
	w.WriteHeader(http.StatusFound)                                  //关键在这里!
}

func TestResponseHeader(t *testing.T) {
	http.HandleFunc("/", login)              //设置访问的路由
	err := http.ListenAndServe(":9090", nil) //设置监听的端口
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

在这里插入图片描述

在这里插入图片描述

测试用例2:模拟一下,重定向到本地的某个接口下

test.go


import (
	"fmt"
	"log"
	"net/http"
	"os"
	"testing"
)

func authorizeHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Print(fmt.Sprintf("%v+", r))
	w.Header().Set("Cache-Control", "must-revalidate, no-store")
	w.Header().Set("Content-Type", " text/html;charset=UTF-8")
	// 模拟重定向到login接口下
	w.Header().Set("Location", "/login") //跳转地址设置
	w.WriteHeader(http.StatusFound)      //关键在这里!
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Println("\n-----打开---Login.html---")
	outputHTML(w, r, "static/login.html")
}

// 测试用例2:模拟一下,重定向到本服务的某个API接口下
func TestResponseHeader2(t *testing.T) {
	http.HandleFunc("/", authorizeHandler)   //设置访问的路由
	http.HandleFunc("/login", loginHandler)  //设置访问的路由
	err := http.ListenAndServe(":9090", nil) //设置监听的端口
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

func outputHTML(w http.ResponseWriter, req *http.Request, filename string) {
	file, err := os.Open(filename)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	defer file.Close()
	fi, _ := file.Stat()
	http.ServeContent(w, req, file.Name(), fi.ModTime(), file)
}

在test.go的同级目录下,
创建一个static目录
在static目录下创建静态文件,如
login.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <h1>Login In</h1>
        <form action="/login" method="POST">
            <div class="form-group">
                <label for="username">User Name</label>
                <input type="text" class="form-control" name="username" required placeholder="Please enter your user name">
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" class="form-control" name="password" placeholder="Please enter your password">
            </div>
            <button type="submit" class="btn btn-success">Login</button>
        </form>
    </div>
</body>

</html>

打开浏览器,输入127.0.0.1:9090
会自动跳转到登录页面

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码二哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值