httptype

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSV

func CSV(v [][]string) httptest.Body

CSV wraps the given value v and returns a Body that represents the value as csv encoded data. The resulting Body uses encoding/csv to encode and decode the given value, see the encoding/csv documentation for more details.

func Header(v interface{}) httptest.HeaderGetter

Header wraps the given value v and returns an httptest.HeaderGetter that can be used in the httptest.Request.Header and httptest.Response.Header fields. The v argument must be a named struct or a pointer to a named struct, otherwise Header will panic.

The returned httptest.HeaderGetter implementation traverses the given struct and adds its fields into an http.Header value using the following rules:

By default the name of the field is used as the http.Header key, however this can be overridden by adding a `header` tag to the field. For example:

Field string `header:"Header-Name"`

An embedded struct field will be traversed recursively.

A non-embedded struct field will be ignored.

Only fields that are exported and have the following types are added to the http.Header value:

  • string
  • []string
  • [N]string
  • <a type whose underlying type is one of the above>
  • <a pointer type to any of the above>

func JSON

func JSON(v interface{}) httptest.Body

JSON wraps the given value v and returns a Body that represents the value as json encoded data. The resulting Body uses encoding/json to encode and decode the given value, see the encoding/json documentation for more details.

func Params

func Params(v interface{}) httptest.ParamSetter

Params wraps the given value v and returns an httptest.ParamSetter that can be used in the httptest.Request.Params field. The v argument must be a named struct or a pointer to a named struct, otherwise Params will panic.

The SetParams(pattern string) (path string) implementation of the returned httptest.ParamSetter traverses the given struct and, using the rules outlined below, replaces any placeholders in the given pattern with the values of the struct's fields.

The placeholders in the given pattern are expected to be demarcated with curly braces. For example:

"/users/{user_id}".

By default the name of the field is used to match a placeholder, however this can be overridden by adding a `param` tag to the field. For example:

ID int `param:"user_id"`

An embedded struct field will be traversed recursively.

A non-embedded struct field will be ignored.

Only fields that are exported and have the following types will be used to replace the placeholders:

  • string
  • bool
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • <types whose underlying type is one of the above>
  • <pointer types to any of the above>

func Query

func Query(v interface{}) httptest.QueryGetter

func Text

func Text(v string) httptest.Body

Text wraps the given value v and returns a Body that represents the value as plain text.

func XML

func XML(v interface{}) httptest.Body

XML wraps the given value v and returns a Body that represents the value as xml encoded data. The resulting Body uses encoding/xml to encode and decode the given value, see the encoding/xml documentation for more details.

Types

type QueryEncoderBody

type QueryEncoderBody interface {
	httptest.QueryGetter
	httptest.Body
}

The QueryEncoderBody is an interface that groups the QueryEncoder and Body interfaces.

func Form

func Form(v interface{}) QueryEncoderBody

Form wraps the given value v and returns a Body that represents the value as form encoded data. At the moment the resulting Body uses github.com/frk/form to encode and decode the given value, see the package's documentation for more details.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL