You've already forked oauth2-proxy
mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-09-16 09:16:33 +02:00
Remove old requests code
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/bitly/go-simplejson"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
|
||||
)
|
||||
|
||||
// Request parses the request body into a simplejson.Json object
|
||||
func Request(req *http.Request) (*simplejson.Json, error) {
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
logger.Printf("%s %s %s", req.Method, req.URL, err)
|
||||
return nil, err
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if body != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
|
||||
logger.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("problem reading http request body: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("got %d %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
data, err := simplejson.NewJson(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error unmarshalling json: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// RequestJSON parses the request body into the given interface
|
||||
func RequestJSON(req *http.Request, v interface{}) error {
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
logger.Printf("%s %s %s", req.Method, req.URL, err)
|
||||
return err
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if body != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
|
||||
logger.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading body from http response: %w", err)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
return fmt.Errorf("got %d %s", resp.StatusCode, body)
|
||||
}
|
||||
return json.Unmarshal(body, v)
|
||||
}
|
||||
|
||||
// RequestUnparsedResponse performs a GET and returns the raw response object
|
||||
func RequestUnparsedResponse(ctx context.Context, url string, header http.Header) (resp *http.Response, err error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error performing get request: %w", err)
|
||||
}
|
||||
req.Header = header
|
||||
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
@@ -1,136 +0,0 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/bitly/go-simplejson"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func testBackend(t *testing.T, responseCode int, payload string) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(responseCode)
|
||||
_, err := w.Write([]byte(payload))
|
||||
require.NoError(t, err)
|
||||
}))
|
||||
}
|
||||
|
||||
func TestRequest(t *testing.T) {
|
||||
backend := testBackend(t, 200, "{\"foo\": \"bar\"}")
|
||||
defer backend.Close()
|
||||
|
||||
req, _ := http.NewRequest("GET", backend.URL, nil)
|
||||
response, err := Request(req)
|
||||
assert.Equal(t, nil, err)
|
||||
result, err := response.Get("foo").String()
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "bar", result)
|
||||
}
|
||||
|
||||
func TestRequestFailure(t *testing.T) {
|
||||
// Create a backend to generate a test URL, then close it to cause a
|
||||
// connection error.
|
||||
backend := testBackend(t, 200, "{\"foo\": \"bar\"}")
|
||||
backend.Close()
|
||||
|
||||
req, err := http.NewRequest("GET", backend.URL, nil)
|
||||
assert.Equal(t, nil, err)
|
||||
resp, err := Request(req)
|
||||
assert.Equal(t, (*simplejson.Json)(nil), resp)
|
||||
assert.NotEqual(t, nil, err)
|
||||
if !strings.Contains(err.Error(), "refused") {
|
||||
t.Error("expected error when a connection fails: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpErrorCode(t *testing.T) {
|
||||
backend := testBackend(t, 404, "{\"foo\": \"bar\"}")
|
||||
defer backend.Close()
|
||||
|
||||
req, err := http.NewRequest("GET", backend.URL, nil)
|
||||
assert.Equal(t, nil, err)
|
||||
resp, err := Request(req)
|
||||
assert.Equal(t, (*simplejson.Json)(nil), resp)
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func TestJsonParsingError(t *testing.T) {
|
||||
backend := testBackend(t, 200, "not well-formed JSON")
|
||||
defer backend.Close()
|
||||
|
||||
req, err := http.NewRequest("GET", backend.URL, nil)
|
||||
assert.Equal(t, nil, err)
|
||||
resp, err := Request(req)
|
||||
assert.Equal(t, (*simplejson.Json)(nil), resp)
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
||||
// Parsing a URL practically never fails, so we won't cover that test case.
|
||||
func TestRequestUnparsedResponseUsingAccessTokenParameter(t *testing.T) {
|
||||
backend := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.FormValue("access_token")
|
||||
if r.URL.Path == "/" && token == "my_token" {
|
||||
w.WriteHeader(200)
|
||||
_, err := w.Write([]byte("some payload"))
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
w.WriteHeader(403)
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
response, err := RequestUnparsedResponse(
|
||||
context.Background(), backend.URL+"?access_token=my_token", nil)
|
||||
assert.Equal(t, nil, err)
|
||||
defer response.Body.Close()
|
||||
|
||||
assert.Equal(t, 200, response.StatusCode)
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "some payload", string(body))
|
||||
}
|
||||
|
||||
func TestRequestUnparsedResponseUsingAccessTokenParameterFailedResponse(t *testing.T) {
|
||||
backend := testBackend(t, 200, "some payload")
|
||||
// Close the backend now to force a request failure.
|
||||
backend.Close()
|
||||
|
||||
response, err := RequestUnparsedResponse(
|
||||
context.Background(), backend.URL+"?access_token=my_token", nil)
|
||||
assert.NotEqual(t, nil, err)
|
||||
assert.Equal(t, (*http.Response)(nil), response)
|
||||
}
|
||||
|
||||
func TestRequestUnparsedResponseUsingHeaders(t *testing.T) {
|
||||
backend := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" && r.Header["Auth"][0] == "my_token" {
|
||||
w.WriteHeader(200)
|
||||
_, err := w.Write([]byte("some payload"))
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
w.WriteHeader(403)
|
||||
}
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
headers := make(http.Header)
|
||||
headers.Set("Auth", "my_token")
|
||||
response, err := RequestUnparsedResponse(context.Background(), backend.URL, headers)
|
||||
assert.Equal(t, nil, err)
|
||||
defer response.Body.Close()
|
||||
|
||||
assert.Equal(t, 200, response.StatusCode)
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
assert.Equal(t, "some payload", string(body))
|
||||
}
|
Reference in New Issue
Block a user