Skip to content

Commit

Permalink
[~] callback enhancements (#63)
Browse files Browse the repository at this point in the history
* [~] callback enhancements

* chore: Updated coverage badge.

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
Noooste and actions-user authored Mar 17, 2024
1 parent 1cc427c commit c9b72d4
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AzureTLS Client
[![GoDoc](https://godoc.org/github.com/Noooste/azuretls-client?status.svg)](https://godoc.org/github.com/Noooste/azuretls-client)
![Coverage](https://img.shields.io/badge/Coverage-79.6%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-79.5%25-brightgreen)
[![build](https://github.com/Noooste/azuretls-client/actions/workflows/push.yml/badge.svg)](https://github.com/Noooste/azuretls-client/actions/workflows/push.yml)
[![Go Report Card](https://goreportcard.com/badge/Noooste/azuretls-client)](https://goreportcard.com/report/Noooste/azuretls-client)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Noooste/azuretls-client/blob/master/LICENSE)
Expand Down
14 changes: 10 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ func (s *Session) prepareRequest(request *Request, args ...any) error {

s.fillEmptyValues(request)

if s.PreHook != nil {
return s.PreHook(request)
}

if s.PreHookWithContext != nil {
return s.PreHookWithContext(&Context{
Session: s,
Request: request,
})
}

if s.PreHook != nil {
return s.PreHook(request)
}

return nil
}

Expand Down Expand Up @@ -135,6 +135,12 @@ func newRequest(ctx context.Context, verbose bool, req *Request) (newReq *http.R
return
}

// SetContext sets the context for the request.
func (r *Request) SetContext(ctx context.Context) {
r.ctx = ctx
}

// Context returns the context for the request.
func (r *Request) Context() context.Context {
return r.ctx
}
2 changes: 2 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (s *Session) buildResponse(response *Response, httpResponse *http.Response)
}

response.StatusCode = httpResponse.StatusCode
response.Status = httpResponse.Status

response.Header = headers

var u *url.URL
Expand Down
20 changes: 18 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (s *Session) SetContext(ctx context.Context) {
s.Connections.SetContext(ctx)
}

func (s *Session) Context() context.Context {
return s.ctx
}

// Ip returns the public IP address of the session
func (s *Session) Ip() (ip string, err error) {
r, err := s.Get("https://api.ipify.org")
Expand Down Expand Up @@ -145,13 +149,21 @@ func (s *Session) send(request *Request) (response *Response, err error) {
}

if s.CallbackWithContext != nil {
s.CallbackWithContext(&Context{
ctx := &Context{
Session: s,
Request: request,
Response: response,
Err: err,
RequestStartTime: request.startTime,
})
}

s.CallbackWithContext(ctx)

if ctx.Err != nil {
err = ctx.Err
}

response = ctx.Response
}
}()

Expand Down Expand Up @@ -457,6 +469,10 @@ func (s *Session) Connect(u string) error {
return nil
}

func (c *Context) Context() context.Context {
return c.ctx
}

// Close closes the session and all its connections.
// It is recommended to call this function when the session is no longer needed.
//
Expand Down
106 changes: 69 additions & 37 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,46 +109,53 @@ type Request struct {
HttpRequest *http.Request
Response *Response

Method string // HTTP method, e.g., GET, POST.
// HTTP method, e.g., GET, POST.
Method string

Url string
parsedUrl *url.URL // Parsed version of Url.
Url string
// Parsed version of Url.
parsedUrl *url.URL

Body any
body []byte

PHeader PHeader
OrderedHeaders OrderedHeaders

Header http.Header // Headers for the request. Deprecated: Use OrderedHeaders instead.
HeaderOrder HeaderOrder // Order of headers for the request.
// Headers for the request. Deprecated: Use OrderedHeaders instead.
Header http.Header
// Order of headers for the request.
HeaderOrder HeaderOrder

conn *Conn // Connection associated with the request.
// Connection associated with the request.
conn *Conn

proxy string
ua string
browser string

DisableRedirects bool // If true, redirects won't be followed.
MaxRedirects uint // Maximum number of redirects to follow.

NoCookie bool // If true, cookies won't be included in the request.

TimeOut time.Duration // Maximum time to wait for request to complete.

IsRedirected bool // Indicates if the current request is a result of a redirection.

InsecureSkipVerify bool // If true, server's certificate is not verified.

IgnoreBody bool // If true, the body of the response is not read.

Proto string
// If true, redirects won't be followed.
DisableRedirects bool
// Maximum number of redirects to follow.
MaxRedirects uint
// If true, cookies won't be included in the request.
NoCookie bool
// Maximum time to wait for request to complete.
TimeOut time.Duration
// Indicates if the current request is a result of a redirection.
IsRedirected bool
// If true, server's certificate is not verified.
InsecureSkipVerify bool

// If true, the body of the response is not read.
IgnoreBody bool
Proto string
ForceHTTP1 bool

ContentLength int64 // Length of content in the request.

ctx context.Context // Context for cancellable and timeout operations.
// Length of content in the request.
ContentLength int64
// Context for cancellable and timeout operations.
ctx context.Context

startTime time.Time

Expand All @@ -159,28 +166,53 @@ type Request struct {
// request. This includes status code, body, headers, cookies, associated
// request details, TLS connection state, etc.
type Response struct {
StatusCode int // HTTP status code, e.g., 200, 404.

Body []byte // Byte representation of the response body.
RawBody io.ReadCloser // Raw body stream.
Header http.Header // Response headers.
Cookies map[string]string // Parsed cookies from the response.
Url string // URL from which the response was received.
IgnoreBody bool // Indicates if the body of the response was ignored.

HttpResponse *http.Response // The underlying HTTP response.
// HTTP status code, e.g., 200, 404.
StatusCode int

Request *Request // Reference to the associated request.
// HTTP status message, e.g., "OK", "Not Found".
Status string

ContentLength int64 // Length of content in the response.
// Byte representation of the response body.
Body []byte
// Raw body stream.
RawBody io.ReadCloser
// Response headers.
Header http.Header
// Parsed cookies from the response.
Cookies map[string]string
// URL from which the response was received.
Url string
// Indicates if the body of the response was ignored.
IgnoreBody bool

// The underlying HTTP response.
HttpResponse *http.Response
// Reference to the associated request.
Request *Request
// Length of content in the response.
ContentLength int64
}

// Context represents the context of a request. It holds the session, request,
// response, error, and other details associated with the request.
type Context struct {
Session *Session
Request *Request
// Session is the session associated with the request.
Session *Session

// Request is the request being made.
Request *Request

// Response is the response received.
// It can be modified to change the response returned by the request.
Response *Response

// Err is the error, if any, that occurred during the request.
// It can be modified to change the error returned by the request.
Err error

// Ctx is the context associated with the request.
ctx context.Context

// RequestStartTime is the time when the request was started.
RequestStartTime time.Time
}
24 changes: 22 additions & 2 deletions test/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,24 @@ func TestSessionCallback(t *testing.T) {
}

session.CallbackWithContext = func(ctx *azuretls.Context) {
withContextCalled = ctx.Session == session && ctx.Request == req
withContextCalled = true
if ctx.Response.Url == "https://www.google.com" {
response, _ := session.Get("https://httpbin.org/get")
ctx.Response = response
}
}

_, err := session.Do(req)
response, err := session.Do(req)
if err != nil {
t.Fatal("TestSessionCallback failed, expected: nil, got: ", err)
return
}

if response.Url != "https://httpbin.org/get" {
t.Fatal("TestSessionCallback failed, expected: https://httpbin.org/get, got: ", response.Url)
return
}

if !called {
t.Fatal("TestSessionCallback failed, expected: called, got: ", called)
return
Expand All @@ -311,6 +320,17 @@ func TestSessionCallback(t *testing.T) {
t.Fatal("TestSessionCallback failed, expected: called, got: ", withContextCalled)
return
}

session.CallbackWithContext = func(ctx *azuretls.Context) {
ctx.Err = errors.New("test")
}

_, err = session.Do(req)

if err == nil {
t.Fatal("TestSessionCallback failed, expected: error, got: nil")
return
}
}

func TestSession_Put(t *testing.T) {
Expand Down

0 comments on commit c9b72d4

Please sign in to comment.