service

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2017 License: Apache-2.0 Imports: 16 Imported by: 0

README

service

[GoDoc] (http://godoc.org/github.com/rtwire/mock/service)

Package service implements a mock RTWire service with HTTP endpoints as described in RTWire's documentation.

The service struct implements http.Handler allowing it to be used in unit/integrations tests with parts of your own code that integrate with RTWire's service. It can be used in combination with the Go client provided by RTWire located here https://github.com/rtwire/go/client. A small example is located below, however more examples are located in the Go client repository.

func TestRTWire(t *testing.T) {
    s := httptest.NewServer(service.New())
    defer s.Close()
	
    cl := client.New(http.DefaultClient, s.URL+"/v1/mainnet", "user", "pass")
    acc, err := cl.CreateAccount()
	if err != nil {
        t.Fatal(err)
    }
    if acc.ID == 0 {
        t.Fatal("uninitialized account")
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(options ...option) *service

New returns a high fidelity mock RTWire service. The *service struct implements http.Handler and exposes HTTP endpoints identical to those described in https://rtwire.com/docs. This means *service can be combined httptest.Server to unit test your RTWire integration code locally. For example:

s := httptest.NewServer(cervice.New())
defer s.Close()

req, err := http.NewRequest("GET", s.URL+"/v1/mainnet/accounts", nil)
req.SetBasicAuth("user", "pass")
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
// Handle error if not nil. resp.Body will contain a JSON object with all
// accounts created so far with the mock service.

This will allow the s.URL to expose the same HTTP endpoints as RTWire's. See https://github.com/rtwire/go/client/ for examples. Importantly this mock service exposes one extra endpoint:

POST /v1/[network]/addresses/[address]

This endpoint can be passed the folliwng JSON object that will credit the account at address [address] with value [value].

   {
     "value": [value]
	  }

Note that you must specify a "Content-Type: application/json" header with this endpoint. Using this endpoint is the equivalent to receiving bitcoins from the network.

The default basic authentication username is user is 'user' and password is 'pass'. Both can be changed by using the UserPass option.

func UserPass added in v1.1.0

func UserPass(network Network, user, pass string) option

UserPass is an option that can be passsed to New() to change the default user and pass authentication credentials for the specified network.

Types

type Network added in v1.1.0

type Network string

Network specifies the Bitcoin network.

const (
	// TestNet3 represents the testnet3 Bitcoin network.
	TestNet3 Network = "testnet3"

	// MainNet represents the mainnet Bitcoin network.
	MainNet Network = "mainnet"
)

Jump to

Keyboard shortcuts

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