Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to activate HTTPS and HTTP/2 if client supports it #82

Merged
merged 1 commit into from
Jul 18, 2018

Conversation

brandur-stripe
Copy link
Contributor

@brandur-stripe brandur-stripe commented Jul 18, 2018

This adds a new -https command line option to stripe-mock. When
enabled, it comes up serving HTTPS using a self-signed certificate
that's bundled into the executable.

Go supports serving HTTP/2 almost automatically from net/http with
HTTPS is activated, so clients that support HTTP/2 can test with it with
stripe-mock. The server will fallback to HTTP automatically for clients
that don't.

I figure this will be handy testing HTTP/2 support in our various client
libraries because it's easy to have stripe-mock spit out debug lines
that tell us that HTTP/2 has been activated correctly.

r? @ob-stripe
cc @stripe/api-libraries

This adds a new `-https` command line option to stripe-mock. When
enabled, it comes up serving HTTPS using a self-signed certificate
that's bundled into the executable.

Go supports serving HTTP/2 almost automatically from `net/http` with
HTTPS is activated, so clients that support HTTP/2 can test with it with
stripe-mock. The server will fallback to HTTP automatically for clients
that don't.

I figure this will be handy testing HTTP/2 support in our various client
libraries because it's easy to have stripe-mock spit out debug lines
that tell us that HTTP/2 has been activated correctly.
@brandur-stripe
Copy link
Contributor Author

Here's a little program that shows how this might be used with stripe-go:

package main

import (
	"crypto/tls"
	"fmt"
	"net/http"

	stripe "github.com/stripe/stripe-go"
	"github.com/stripe/stripe-go/customer"
	"golang.org/x/net/http2"
)

func main() {
	stripe.Key = "sk_test_123"

	transport := &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}

	err := http2.ConfigureTransport(transport)
	if err != nil {
		panic(err)
	}

	httpClient := &http.Client{
		Transport: transport,
	}

	stripeMockBackend := stripe.GetBackendWithConfig(
		stripe.APIBackend,
		&stripe.BackendConfig{
			URL:        "https://localhost:12112",
			HTTPClient: httpClient,
			Logger:     stripe.Logger,
		},
	)
	stripe.SetBackend(stripe.APIBackend, stripeMockBackend)
	stripe.SetBackend(stripe.UploadsBackend, stripeMockBackend)

	c, err := customer.Get("cus_DDHV5EAh4nMeEf", nil)

	fmt.Printf("customer = %+v\n", c)
	fmt.Printf("err = %+v\n", err)
}

And the output from a run:

$ GODEBUG=http2debug=1 go run http2_check.go
2018/07/17 17:22:45 http2: Transport failed to get client conn for localhost:12112: http2: no cached connection was available
2018/07/17 17:22:45 http2: Transport creating client conn 0xc42019e000 to [::1]:12112
2018/07/17 17:22:45 http2: Transport encoding header ":authority" = "localhost:12112"
2018/07/17 17:22:45 http2: Transport encoding header ":method" = "GET"
2018/07/17 17:22:45 http2: Transport encoding header ":path" = "/v1/customers/cus_DDHV5EAh4nMeEf"
2018/07/17 17:22:45 http2: Transport encoding header ":scheme" = "https"
2018/07/17 17:22:45 http2: Transport encoding header "authorization" = "Bearer sk_test_123"
2018/07/17 17:22:45 http2: Transport encoding header "stripe-version" = "2018-02-06"
2018/07/17 17:22:45 http2: Transport encoding header "user-agent" = "Stripe/v1 GoBindings/36.1.1"
2018/07/17 17:22:45 http2: Transport encoding header "content-type" = "application/x-www-form-urlencoded"
2018/07/17 17:22:45 http2: Transport encoding header "x-stripe-client-user-agent" = "{\"application\":null,\"bindings_version\":\"36.1.1\",\"lang\":\"go\",\"lang_version\":\"go1.10.1\",\"publisher\":\"stripe\",\"uname\":\"Darwin st-brandur2.local 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64\\n\"}"
2018/07/17 17:22:45 http2: Transport encoding header "accept-encoding" = "gzip"
2018/07/17 17:22:45 http2: Transport received SETTINGS len=24, settings: MAX_FRAME_SIZE=1048576, MAX_CONCURRENT_STREAMS=250, MAX_HEADER_LIST_SIZE=1048896, INITIAL_WINDOW_SIZE=1048576
2018/07/17 17:22:45 http2: Transport received WINDOW_UPDATE len=4 (conn) incr=983041
2018/07/17 17:22:45 http2: Transport received SETTINGS flags=ACK len=0
2018/07/17 17:22:45 http2: Transport received HEADERS flags=END_HEADERS stream=1 len=86
2018/07/17 17:22:45 http2: Transport received DATA flags=END_STREAM stream=1 len=2070 data="{\"account_balance\":0,\"created\":1234567890,\"currency\":\"usd\",\"default_source\":null,\"delinquent\":false,\"description\":null,\"discount\":null,\"email\":null,\"id\":\"cus_DDHV5EAh4nMeEf\",\"invoice_prefix\":\"63FC18E\",\"livemode\":false,\"metadata\":{},\"object\":\"customer\",\"shi" (1814 bytes omitted)
customer = &{AccountBalance:0 BusinessVATID: Created:1234567890 Currency:usd DefaultSource:<nil> Deleted:false Delinquent:false Description: Discount:<nil> Email: ID:cus_DDHV5EAh4nMeEf Livemode:false Metadata:map[] Shipping:<nil> Sources:0xc420099f50 Subscriptions:0xc4201de120}
err = <nil>

And stripe-mock's output:

2018/07/17 17:23:15 http2: server connection from [::1]:53049 on 0xc4208c3ee0
2018/07/17 17:23:15 http2: server: client [::1]:53049 said hello
2018/07/17 17:23:15 http2: server read frame SETTINGS len=18, settings: ENABLE_PUSH=0, INITIAL_WINDOW_SIZE=4194304, MAX_HEADER_LIST_SIZE=10485760
2018/07/17 17:23:15 http2: server processing setting [ENABLE_PUSH = 0]
2018/07/17 17:23:15 http2: server processing setting [INITIAL_WINDOW_SIZE = 4194304]
2018/07/17 17:23:15 http2: server processing setting [MAX_HEADER_LIST_SIZE = 10485760]
2018/07/17 17:23:15 http2: server read frame WINDOW_UPDATE len=4 (conn) incr=1073741824
2018/07/17 17:23:15 http2: server read frame HEADERS flags=END_STREAM|END_HEADERS stream=1 len=353
Request: GET /v1/customers/cus_DDHV5EAh4nMeEf
2018/07/17 17:23:15 http2: server read frame SETTINGS flags=ACK len=0
Response: elapsed=320.911µs status=200
2018/07/17 17:23:15 http2: server encoding header ":status" = "200"
2018/07/17 17:23:15 http2: server encoding header "request-id" = "req_123"
2018/07/17 17:23:15 http2: server encoding header "stripe-mock-version" = "master"
2018/07/17 17:23:15 http2: server encoding header "content-type" = "text/plain; charset=utf-8"
2018/07/17 17:23:15 http2: server encoding header "content-length" = "2070"
2018/07/17 17:23:15 http2: server encoding header "date" = "Wed, 18 Jul 2018 00:23:15 GMT"

Copy link
Contributor

@ob-stripe ob-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/ LGTM!

@brandur-stripe
Copy link
Contributor Author

Thanks OB!

@brandur-stripe brandur-stripe merged commit 60751ba into master Jul 18, 2018
@brandur-stripe brandur-stripe deleted the brandur-http2 branch July 18, 2018 13:46
@brandur-stripe
Copy link
Contributor Author

Released as v0.20.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants