forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 8
proxy support #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
proxy support #21
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,74 @@ | ||
| // Copyright 2018 Drone.IO Inc. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package proxy | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/url" | ||
| ) | ||
|
|
||
| // Transport is an http.RoundTripper that makes HTTP | ||
| // requests through a proxy, wrapping a base RoundTripper | ||
| type Transport struct { | ||
| Base http.RoundTripper | ||
| ProxyURL *url.URL | ||
| } | ||
|
|
||
| // RoundTrip makes the request through the configured proxy. | ||
| func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) { | ||
| // If no proxy is configured, use the base transport | ||
| if t.ProxyURL == nil { | ||
| return t.base().RoundTrip(r) | ||
| } | ||
|
|
||
| // Create a new transport with the proxy configuration | ||
| proxyTransport := &http.Transport{ | ||
| Proxy: func(_ *http.Request) (*url.URL, error) { | ||
| return t.ProxyURL, nil | ||
| }, | ||
| } | ||
|
|
||
| // If we have a base transport, copy its configuration | ||
| if t.Base != nil { | ||
| if baseTransport, ok := t.Base.(*http.Transport); ok { | ||
| proxyTransport.TLSClientConfig = baseTransport.TLSClientConfig | ||
| proxyTransport.DialContext = baseTransport.DialContext | ||
| proxyTransport.MaxIdleConns = baseTransport.MaxIdleConns | ||
| proxyTransport.MaxIdleConnsPerHost = baseTransport.MaxIdleConnsPerHost | ||
| proxyTransport.IdleConnTimeout = baseTransport.IdleConnTimeout | ||
| proxyTransport.TLSHandshakeTimeout = baseTransport.TLSHandshakeTimeout | ||
| proxyTransport.ExpectContinueTimeout = baseTransport.ExpectContinueTimeout | ||
| } | ||
| } | ||
|
|
||
| return proxyTransport.RoundTrip(r) | ||
rcchopra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // base returns the base transport. If no base transport | ||
| // is configured, the default transport is returned. | ||
| func (t *Transport) base() http.RoundTripper { | ||
| if t.Base != nil { | ||
| return t.Base | ||
| } | ||
| return http.DefaultTransport | ||
| } | ||
|
|
||
| // NewTransport creates a new proxy transport with the given proxy URL. | ||
| // If proxyURL is empty or nil, it returns the base transport unchanged. | ||
| func NewTransport(base http.RoundTripper, proxyURL string) (http.RoundTripper, error) { | ||
| if proxyURL == "" { | ||
| return base, nil | ||
| } | ||
|
|
||
| parsedURL, err := url.Parse(proxyURL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &Transport{ | ||
| Base: base, | ||
| ProxyURL: parsedURL, | ||
| }, nil | ||
| } | ||
This file contains hidden or 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,97 @@ | ||
| // Copyright 2018 Drone.IO Inc. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package proxy | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNewTransport_EmptyProxyURL(t *testing.T) { | ||
| base := http.DefaultTransport | ||
| transport, err := NewTransport(base, "") | ||
| if err != nil { | ||
| t.Fatalf("Expected no error for empty proxy URL, got: %v", err) | ||
| } | ||
| if transport != base { | ||
| t.Error("Expected transport to be the same as base for empty proxy URL") | ||
| } | ||
| } | ||
|
|
||
| func TestNewTransport_InvalidProxyURL(t *testing.T) { | ||
| base := http.DefaultTransport | ||
| _, err := NewTransport(base, "://invalid") | ||
| if err == nil { | ||
| t.Error("Expected error for invalid proxy URL") | ||
| } | ||
| } | ||
|
|
||
| func TestNewTransport_ValidProxyURL(t *testing.T) { | ||
| base := http.DefaultTransport | ||
| proxyURL := "http://proxy.example.com:8080" | ||
| transport, err := NewTransport(base, proxyURL) | ||
| if err != nil { | ||
| t.Fatalf("Expected no error for valid proxy URL, got: %v", err) | ||
| } | ||
|
|
||
| proxyTransport, ok := transport.(*Transport) | ||
| if !ok { | ||
| t.Fatal("Expected transport to be of type *Transport") | ||
| } | ||
|
|
||
| expectedURL, _ := url.Parse(proxyURL) | ||
| if proxyTransport.ProxyURL.String() != expectedURL.String() { | ||
| t.Errorf("Expected proxy URL %s, got %s", expectedURL.String(), proxyTransport.ProxyURL.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestTransport_RoundTrip_NoProxy(t *testing.T) { | ||
| // Create a test server | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte("OK")) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| transport := &Transport{ | ||
| Base: http.DefaultTransport, | ||
| ProxyURL: nil, | ||
| } | ||
|
|
||
| client := &http.Client{Transport: transport} | ||
| resp, err := client.Get(server.URL) | ||
| if err != nil { | ||
| t.Fatalf("Expected no error, got: %v", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| t.Errorf("Expected status OK, got: %d", resp.StatusCode) | ||
| } | ||
| } | ||
|
|
||
| func TestTransport_RoundTrip_WithProxy(t *testing.T) { | ||
| // Create a test server | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte("OK")) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| proxyURL, _ := url.Parse("http://proxy.example.com:8080") | ||
| transport := &Transport{ | ||
| Base: http.DefaultTransport, | ||
| ProxyURL: proxyURL, | ||
| } | ||
|
|
||
| // This test verifies that the transport is configured with the proxy | ||
| // The actual proxy behavior would require a real proxy server for testing | ||
| // We're just ensuring the transport is properly configured | ||
| if transport.ProxyURL.String() != proxyURL.String() { | ||
| t.Errorf("Expected proxy URL %s, got %s", proxyURL.String(), transport.ProxyURL.String()) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.