Skip to content

Commit

Permalink
Moves embeds out of package main, so this can be started as an *httpt…
Browse files Browse the repository at this point in the history
…est.Server (#364)

* Moves embeds out of package main, so this can be started as an *httptest.Server
  • Loading branch information
zerbitx committed Feb 8, 2023
1 parent 80e39f8 commit ee1a346
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Makefile
Expand Up @@ -28,8 +28,8 @@ docker-run:
.PHONY: docker-run

update-openapi-spec:
rm -f ./openapi/spec3.json
rm -f ./openapi/fixtures3.json
wget https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.json -P ./openapi
wget https://raw.githubusercontent.com/stripe/openapi/master/openapi/fixtures3.json -P ./openapi
rm -f ./embedded/openapi/spec3.json
rm -f ./embedded/openapi/fixtures3.json
wget https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.json -P ./embedded/openapi
wget https://raw.githubusercontent.com/stripe/openapi/master/openapi/fixtures3.json -P ./embedded/openapi
.PHONY: update-openapi-spec
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions embedded/embedded.go
@@ -0,0 +1,17 @@
package embedded

import (
_ "embed"
)

//go:embed cert/key.pem
var CertKey []byte

//go:embed cert/cert.pem
var CertCert []byte

//go:embed openapi/fixtures3.json
var OpenAPIFixtures []byte

//go:embed openapi/spec3.json
var OpenAPISpec []byte
File renamed without changes.
File renamed without changes.
19 changes: 4 additions & 15 deletions main.go
Expand Up @@ -5,6 +5,7 @@ import (
_ "embed"
"flag"
"fmt"
"github.com/stripe/stripe-mock/embedded"
"net"
"net/http"
"os"
Expand All @@ -13,18 +14,6 @@ import (
"github.com/stripe/stripe-mock/server"
)

//go:embed cert/key.pem
var EmbeddedCertKey []byte

//go:embed cert/cert.pem
var EmbeddedCertCert []byte

//go:embed openapi/fixtures3.json
var EmbeddedOpenAPIFixtures []byte

//go:embed openapi/spec3.json
var EmbeddedOpenAPISpec []byte

const defaultPortHTTP = 12111
const defaultPortHTTPS = 12112

Expand Down Expand Up @@ -90,12 +79,12 @@ func main() {
// For both spec and fixtures stripe-mock will by default load data from
// internal assets compiled into the binary, but either one can be
// overridden with a -spec or -fixtures argument and a path to a file.
stripeSpec, err := server.LoadSpec(EmbeddedOpenAPISpec, options.specPath)
stripeSpec, err := server.LoadSpec(embedded.OpenAPISpec, options.specPath)
if err != nil {
abort(err.Error())
}

fixtures, err := server.LoadFixtures(EmbeddedOpenAPIFixtures, options.fixturesPath)
fixtures, err := server.LoadFixtures(embedded.OpenAPIFixtures, options.fixturesPath)
if err != nil {
abort(err.Error())
}
Expand Down Expand Up @@ -354,7 +343,7 @@ func abort(message string) {

// getTLSCertificate reads a certificate and key embedded into the binary
func getTLSCertificate() (tls.Certificate, error) {
return tls.X509KeyPair(EmbeddedCertCert, EmbeddedCertKey)
return tls.X509KeyPair(embedded.CertCert, embedded.CertKey)
}

func getPortListener(addr string, protocol string) (net.Listener, error) {
Expand Down
4 changes: 2 additions & 2 deletions server/server_test.go
Expand Up @@ -46,8 +46,8 @@ func init() {
func initRealSpec() {
// Determine test file name and paths to openapi specs
_, testFile, _, _ := runtime.Caller(0)
specFile := path.Join(path.Dir(testFile), "../openapi/spec3.json")
fixtureFile := path.Join(path.Dir(testFile), "../openapi/fixtures3.json")
specFile := path.Join(path.Dir(testFile), "../embedded/openapi/spec3.json")
fixtureFile := path.Join(path.Dir(testFile), "../embedded/openapi/fixtures3.json")

// Load the spec information
data, err := ioutil.ReadFile(specFile)
Expand Down

0 comments on commit ee1a346

Please sign in to comment.