| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package service
- import (
- "net/url"
- "testing"
- )
- func TestProxyURL(t *testing.T) {
- t.Parallel()
- tests := []struct {
- name string
- proxy Proxy
- want string
- }{
- {
- name: "without auth",
- proxy: Proxy{
- Protocol: "http",
- Host: "proxy.example.com",
- Port: 8080,
- },
- want: "http://proxy.example.com:8080",
- },
- {
- name: "with auth",
- proxy: Proxy{
- Protocol: "socks5",
- Host: "socks.example.com",
- Port: 1080,
- Username: "user",
- Password: "pass",
- },
- want: "socks5://user:pass@socks.example.com:1080",
- },
- {
- name: "username only",
- proxy: Proxy{
- Protocol: "http",
- Host: "proxy.example.com",
- Port: 8080,
- Username: "user-only",
- },
- want: "http://user-only@proxy.example.com:8080",
- },
- {
- name: "password only",
- proxy: Proxy{
- Protocol: "http",
- Host: "proxy.example.com",
- Port: 8080,
- Password: "pass-only",
- },
- want: "http://:pass-only@proxy.example.com:8080",
- },
- {
- name: "with special characters in credentials",
- proxy: Proxy{
- Protocol: "http",
- Host: "proxy.example.com",
- Port: 3128,
- Username: "first last@corp",
- Password: "p@ ss:#word",
- },
- want: "http://first%20last%40corp:p%40%20ss%3A%23word@proxy.example.com:3128",
- },
- }
- for _, tc := range tests {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel()
- if got := tc.proxy.URL(); got != tc.want {
- t.Fatalf("Proxy.URL() mismatch: got=%q want=%q", got, tc.want)
- }
- })
- }
- }
- func TestProxyURL_SpecialCharactersRoundTrip(t *testing.T) {
- t.Parallel()
- proxy := Proxy{
- Protocol: "http",
- Host: "proxy.example.com",
- Port: 3128,
- Username: "first last@corp",
- Password: "p@ ss:#word",
- }
- parsed, err := url.Parse(proxy.URL())
- if err != nil {
- t.Fatalf("parse proxy URL failed: %v", err)
- }
- if got := parsed.User.Username(); got != proxy.Username {
- t.Fatalf("username mismatch after parse: got=%q want=%q", got, proxy.Username)
- }
- pass, ok := parsed.User.Password()
- if !ok {
- t.Fatal("password missing after parse")
- }
- if pass != proxy.Password {
- t.Fatalf("password mismatch after parse: got=%q want=%q", pass, proxy.Password)
- }
- }
|