...

Source file src/github.com/playwright-community/playwright-go/route.go

Documentation: github.com/playwright-community/playwright-go

     1  package playwright
     2  
     3  import (
     4  	"encoding/base64"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"reflect"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  type routeImpl struct {
    13  	channelOwner
    14  }
    15  
    16  func (r *routeImpl) Request() Request {
    17  	return fromChannel(r.initializer["request"]).(*requestImpl)
    18  }
    19  
    20  func unpackOptionalArgument(input interface{}) interface{} {
    21  	inputValue := reflect.ValueOf(input)
    22  	if inputValue.Kind() != reflect.Slice {
    23  		panic("Needs to be a slice")
    24  	}
    25  	if inputValue.Len() == 0 {
    26  		return Null()
    27  	}
    28  	return inputValue.Index(0).Interface()
    29  }
    30  
    31  func (r *routeImpl) Abort(errorCode ...string) error {
    32  	_, err := r.channel.Send("abort", map[string]interface{}{
    33  		"errorCode": unpackOptionalArgument(errorCode),
    34  	})
    35  	return err
    36  }
    37  
    38  func (r *routeImpl) Fulfill(options RouteFulfillOptions) error {
    39  	length := 0
    40  	isBase64 := false
    41  	var fileContentType string
    42  	if _, ok := options.Body.(string); ok {
    43  		isBase64 = false
    44  	} else if body, ok := options.Body.([]byte); ok {
    45  		options.Body = base64.StdEncoding.EncodeToString(body)
    46  		length = len(body)
    47  		isBase64 = true
    48  	} else if options.Path != nil {
    49  		content, err := ioutil.ReadFile(*options.Path)
    50  		if err != nil {
    51  			return err
    52  		}
    53  		fileContentType = http.DetectContentType(content)
    54  		options.Body = base64.StdEncoding.EncodeToString(content)
    55  		isBase64 = true
    56  		length = len(content)
    57  	}
    58  
    59  	headers := make(map[string]string)
    60  	if options.Headers != nil {
    61  		for key, val := range options.Headers {
    62  			headers[strings.ToLower(key)] = val
    63  		}
    64  		options.Headers = nil
    65  	}
    66  	if options.ContentType != nil {
    67  		headers["content-type"] = *options.ContentType
    68  	} else if options.Path != nil {
    69  		headers["content-type"] = fileContentType
    70  	}
    71  	if _, ok := headers["content-length"]; !ok && length > 0 {
    72  		headers["content-length"] = strconv.Itoa(length)
    73  	}
    74  
    75  	options.Path = nil
    76  	_, err := r.channel.Send("fulfill", options, map[string]interface{}{
    77  		"isBase64": isBase64,
    78  		"headers":  serializeMapToNameAndValue(headers),
    79  	})
    80  	return err
    81  }
    82  
    83  func (r *routeImpl) Continue(options ...RouteContinueOptions) error {
    84  	overrides := make(map[string]interface{})
    85  	if len(options) == 1 {
    86  		option := options[0]
    87  		if option.URL != nil {
    88  			overrides["url"] = option.URL
    89  		}
    90  		if option.Method != nil {
    91  			overrides["method"] = option.Method
    92  		}
    93  		if option.Headers != nil {
    94  			overrides["headers"] = serializeMapToNameAndValue(option.Headers)
    95  		}
    96  		if option.PostData != nil {
    97  			switch v := option.PostData.(type) {
    98  			case string:
    99  				overrides["postData"] = base64.StdEncoding.EncodeToString([]byte(v))
   100  			case []byte:
   101  				overrides["postData"] = base64.StdEncoding.EncodeToString(v)
   102  			}
   103  		}
   104  	}
   105  	_, err := r.channel.Send("continue", overrides)
   106  	return err
   107  }
   108  
   109  func newRoute(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *routeImpl {
   110  	bt := &routeImpl{}
   111  	bt.createChannelOwner(bt, parent, objectType, guid, initializer)
   112  	return bt
   113  }
   114  

View as plain text