forked from lqqyt2423/go-mitmproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
package addon | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/lqqyt2423/go-mitmproxy/proxy" | ||
) | ||
|
||
func TestMapItemMatch(t *testing.T) { | ||
req := &proxy.Request{ | ||
Method: "GET", | ||
URL: &url.URL{ | ||
Scheme: "https", | ||
Host: "example.com", | ||
Path: "/path/to/resource", | ||
}, | ||
} | ||
|
||
// test match | ||
|
||
item := &mapItem{ | ||
From: &mapFrom{ | ||
Protocol: "https", | ||
Host: "example.com", | ||
Method: []string{"GET", "POST"}, | ||
Path: "/path/to/resource", | ||
}, | ||
To: nil, | ||
Enable: true, | ||
} | ||
result := item.match(req) | ||
if !result { | ||
t.Errorf("Expected true, but got false") | ||
} | ||
|
||
// empty Protocol and empty Method match | ||
item.From = &mapFrom{ | ||
Protocol: "", | ||
Host: "example.com", | ||
Method: []string{}, | ||
Path: "/path/to/resource", | ||
} | ||
result = item.match(req) | ||
if !result { | ||
t.Errorf("Expected true, but got false") | ||
} | ||
|
||
// empty Host match | ||
item.From = &mapFrom{ | ||
Protocol: "", | ||
Host: "", | ||
Method: []string{}, | ||
Path: "/path/to/*", | ||
} | ||
result = item.match(req) | ||
if !result { | ||
t.Errorf("Expected true, but got false") | ||
} | ||
|
||
// all empty match | ||
item.From = &mapFrom{ | ||
Protocol: "", | ||
Host: "", | ||
Method: []string{}, | ||
Path: "", | ||
} | ||
result = item.match(req) | ||
if !result { | ||
t.Errorf("Expected true, but got false") | ||
} | ||
|
||
// test not match | ||
|
||
// diff Protocol | ||
item.From = &mapFrom{ | ||
Protocol: "http", | ||
Host: "example.com", | ||
Method: []string{}, | ||
Path: "/path/to/resource", | ||
} | ||
result = item.match(req) | ||
if result { | ||
t.Errorf("Expected true, but got false") | ||
} | ||
|
||
// diff Host | ||
item.From = &mapFrom{ | ||
Protocol: "https", | ||
Host: "hello.com", | ||
Method: []string{}, | ||
Path: "/path/to/resource", | ||
} | ||
result = item.match(req) | ||
if result { | ||
t.Errorf("Expected true, but got false") | ||
} | ||
|
||
// diff Method | ||
item.From = &mapFrom{ | ||
Protocol: "https", | ||
Host: "example.com", | ||
Method: []string{"PUT"}, | ||
Path: "/path/to/resource", | ||
} | ||
result = item.match(req) | ||
if result { | ||
t.Errorf("Expected true, but got false") | ||
} | ||
|
||
// diff Path | ||
item.From = &mapFrom{ | ||
Protocol: "http", | ||
Host: "example.com", | ||
Method: []string{}, | ||
Path: "/hello/world", | ||
} | ||
result = item.match(req) | ||
if result { | ||
t.Errorf("Expected true, but got false") | ||
} | ||
} | ||
|
||
func TestMapItemReplace(t *testing.T) { | ||
rawreq := func() *proxy.Request { | ||
return &proxy.Request{ | ||
Method: "GET", | ||
URL: &url.URL{ | ||
Scheme: "https", | ||
Host: "example.com", | ||
Path: "/path/to/resource", | ||
}, | ||
} | ||
} | ||
|
||
item := &mapItem{ | ||
From: &mapFrom{ | ||
Protocol: "https", | ||
Host: "example.com", | ||
Method: []string{"GET", "POST"}, | ||
Path: "/path/to/resource", | ||
}, | ||
To: &mapTo{ | ||
Protocol: "http", | ||
Host: "hello.com", | ||
Path: "", | ||
}, | ||
Enable: true, | ||
} | ||
req := item.replace(rawreq()) | ||
should := "http:https://hello.com/path/to/resource" | ||
if req.URL.String() != should { | ||
t.Errorf("Expected %v, but got %v", should, req.URL.String()) | ||
} | ||
|
||
item = &mapItem{ | ||
From: &mapFrom{ | ||
Protocol: "https", | ||
Host: "example.com", | ||
Method: []string{"GET", "POST"}, | ||
Path: "/path/to/resource", | ||
}, | ||
To: &mapTo{ | ||
Protocol: "http", | ||
Host: "hello.com", | ||
Path: "/path/to/resource", | ||
}, | ||
Enable: true, | ||
} | ||
req = item.replace(rawreq()) | ||
should = "http:https://hello.com/path/to/resource" | ||
if req.URL.String() != should { | ||
t.Errorf("Expected %v, but got %v", should, req.URL.String()) | ||
} | ||
|
||
item = &mapItem{ | ||
From: &mapFrom{ | ||
Protocol: "https", | ||
Host: "example.com", | ||
Method: []string{"GET", "POST"}, | ||
Path: "/path/to/resource", | ||
}, | ||
To: &mapTo{ | ||
Protocol: "http", | ||
Host: "hello.com", | ||
Path: "/path/to/world", | ||
}, | ||
Enable: true, | ||
} | ||
req = item.replace(rawreq()) | ||
should = "http:https://hello.com/path/to/world" | ||
if req.URL.String() != should { | ||
t.Errorf("Expected %v, but got %v", should, req.URL.String()) | ||
} | ||
|
||
item = &mapItem{ | ||
From: &mapFrom{ | ||
Protocol: "https", | ||
Host: "example.com", | ||
Method: []string{"GET", "POST"}, | ||
Path: "/path/to/*", | ||
}, | ||
To: &mapTo{ | ||
Protocol: "http", | ||
Host: "hello.com", | ||
Path: "", | ||
}, | ||
Enable: true, | ||
} | ||
req = item.replace(rawreq()) | ||
should = "http:https://hello.com/path/to/resource" | ||
if req.URL.String() != should { | ||
t.Errorf("Expected %v, but got %v", should, req.URL.String()) | ||
} | ||
|
||
item = &mapItem{ | ||
From: &mapFrom{ | ||
Protocol: "https", | ||
Host: "example.com", | ||
Method: []string{"GET", "POST"}, | ||
Path: "/path/to/*", | ||
}, | ||
To: &mapTo{ | ||
Protocol: "http", | ||
Host: "hello.com", | ||
Path: "/world", | ||
}, | ||
Enable: true, | ||
} | ||
req = item.replace(rawreq()) | ||
should = "http:https://hello.com/world/resource" | ||
if req.URL.String() != should { | ||
t.Errorf("Expected %v, but got %v", should, req.URL.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters