Skip to content

Commit

Permalink
[其他] 测试 follow redirect 是否有效
Browse files Browse the repository at this point in the history
  • Loading branch information
4ra1n committed Sep 10, 2024
1 parent c11ee22 commit a0a42f9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [优化] 使用 `interactsh` 反连随机选择可用的服务器
- [优化] 开启 `debug` 时仅详细打印来自 `poc` 的请求
- [优化] 开启 `debug` 时请求响应限制打印长度
- [其他] 测试 `follow redirect` 是否有效

感谢以下用户的贡献:

Expand Down
14 changes: 0 additions & 14 deletions rawhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,3 @@ func TestRawHttpsProxy(t *testing.T) {
fmt.Println("HTTP Response:\n", string(response.RawResponse))
}
}

func TestRawHttpTE(t *testing.T) {
client, _ := NewHTTPClient(5*time.Second, DefaultNoProxy)
req, err := NewRequest("http://127.0.0.1:8081", MethodGet)
if err != nil {
panic(err)
}
response, err := client.DoReq(req)
if err != nil {
panic(err)
} else {
fmt.Println("HTTP Response:\n", string(response.RawResponse))
}
}
94 changes: 94 additions & 0 deletions rawhttp/redirect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* poc-runner project
* Copyright (C) 2024 4ra1n
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package rawhttp

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

func TestHTTPClientRedirectTrue(t *testing.T) {
go func() {
http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/test", http.StatusFound)
})
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://example.com", http.StatusFound)
})
err := http.ListenAndServe(":10302", nil)
if err != nil {
panic(err)
}
}()

time.Sleep(time.Second)

client, _ := NewHTTPClient(5*time.Second, DefaultNoProxy)
req, err := NewRequest("http://127.0.0.1:10302/redirect", MethodGet)
if err != nil {
panic(err)
}
req.SetFollowRedirect(true)
response, err := client.DoReq(req)
if err != nil {
panic(err)
} else {
fmt.Println("HTTP Response:\n", string(response.RawResponse))
}

if response.StatusCode != 200 {
panic("test error")
}
}

func TestHTTPClientRedirectFalse(t *testing.T) {
go func() {
http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/test", http.StatusFound)
})
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://example.com", http.StatusFound)
})
err := http.ListenAndServe(":10302", nil)
if err != nil {
panic(err)
}
}()

time.Sleep(time.Second)

client, _ := NewHTTPClient(5*time.Second, DefaultNoProxy)
req, err := NewRequest("http://127.0.0.1:10302/redirect", MethodGet)
if err != nil {
panic(err)
}
req.SetFollowRedirect(false)
response, err := client.DoReq(req)
if err != nil {
panic(err)
} else {
fmt.Println("HTTP Response:\n", string(response.RawResponse))
}

if response.StatusCode != 302 {
panic("test error")
}
}

0 comments on commit a0a42f9

Please sign in to comment.