1717package tools
1818
1919import (
20+ "strings"
2021 "testing"
2122 "time"
2223
@@ -39,6 +40,7 @@ func TestFinalizeURL(t *testing.T) {
3940 {name : "adds graphql suffix" , in : "http://localhost:12800" , want : "http://localhost:12800/graphql" },
4041 {name : "trims trailing slash" , in : "http://localhost:12800/" , want : "http://localhost:12800/graphql" },
4142 {name : "keeps existing graphql" , in : "http://localhost:12800/graphql" , want : "http://localhost:12800/graphql" },
43+ {name : "preserves query string" , in : "http://localhost:12800?x=1" , want : "http://localhost:12800/graphql?x=1" },
4244 }
4345
4446 for _ , tc := range tests {
@@ -50,6 +52,44 @@ func TestFinalizeURL(t *testing.T) {
5052 }
5153}
5254
55+ func TestNormalizeOAPURL (t * testing.T ) {
56+ tests := []struct {
57+ name string
58+ in string
59+ want string
60+ wantErr string
61+ }{
62+ {name : "http" , in : "http://localhost:12800" , want : "http://localhost:12800/graphql" },
63+ {name : "https" , in : "https://localhost:12800/graphql" , want : "https://localhost:12800/graphql" },
64+ {name : "preserves query and fragment" , in : "https://localhost:12800/oap?debug=1#frag" , want : "https://localhost:12800/oap/graphql?debug=1#frag" },
65+ {name : "rejects unsupported scheme" , in : "ftp://localhost:12800" , wantErr : "unsupported OAP URL scheme \" ftp\" " },
66+ {name : "rejects missing host" , in : "http://" , wantErr : "host is required" },
67+ {name : "rejects malformed hostless path" , in : "http:/foo" , wantErr : "host is required" },
68+ }
69+
70+ for _ , tc := range tests {
71+ t .Run (tc .name , func (t * testing.T ) {
72+ got , err := NormalizeOAPURL (tc .in )
73+ if tc .wantErr != "" {
74+ if err == nil {
75+ t .Fatalf ("NormalizeOAPURL(%q) error = nil, want %q" , tc .in , tc .wantErr )
76+ }
77+ if ! strings .Contains (err .Error (), tc .wantErr ) {
78+ t .Fatalf ("NormalizeOAPURL(%q) error = %q, want substring %q" , tc .in , err .Error (), tc .wantErr )
79+ }
80+ return
81+ }
82+
83+ if err != nil {
84+ t .Fatalf ("NormalizeOAPURL(%q) unexpected error: %v" , tc .in , err )
85+ }
86+ if got != tc .want {
87+ t .Fatalf ("NormalizeOAPURL(%q) = %q, want %q" , tc .in , got , tc .want )
88+ }
89+ })
90+ }
91+ }
92+
5393func TestParseTimezoneOffset (t * testing.T ) {
5494 loc , ok := parseTimezoneOffset ("+0830" )
5595 if ! ok {
0 commit comments