github.com/companieshouse/lfp-pay-api@v0.0.0-20230203133422-0ca455cd79f9/service/company_service_test.go (about) 1 package service 2 3 import ( 4 "context" 5 "net/http" 6 "testing" 7 8 "github.com/companieshouse/go-session-handler/httpsession" 9 "github.com/companieshouse/go-session-handler/session" 10 "github.com/jarcoal/httpmock" 11 12 . "github.com/smartystreets/goconvey/convey" 13 ) 14 15 var companyDetailsResponse = ` 16 { 17 "company_name": "Test Company", 18 "registered_office_address" : { 19 "postal_code" : "SW1P 1JP", 20 "address_line_2" : "London", 21 "address_line_1" : "100 Rochester Row" 22 } 23 } 24 ` 25 26 func TestUnitGetCompanyInformation(t *testing.T) { 27 28 Convey("GetCompanyNameFromCompanyProfileAPI", t, func() { 29 30 apiURL := "https://api.companieshouse.gov.uk" 31 32 httpmock.Activate() 33 defer httpmock.DeactivateAndReset() 34 ctx := context.WithValue(context.Background(), httpsession.ContextKeySession, &session.Session{}) 35 r := &http.Request{} 36 r = r.WithContext(ctx) 37 38 Convey("invalid request", func() { 39 defer httpmock.Reset() 40 httpmock.RegisterResponder(http.MethodGet, apiURL+"/company/12345678", httpmock.NewStringResponder(http.StatusTeapot, "")) 41 42 resp, err := GetCompanyName("12345678", &http.Request{}) 43 So(resp, ShouldBeEmpty) 44 So(err, ShouldNotBeNil) 45 So(err.Error(), ShouldEqual, `ch-api: got HTTP response code 418 with body: `) 46 }) 47 48 Convey("it returns a serialised version of the response", func() { 49 defer httpmock.Reset() 50 httpmock.RegisterResponder(http.MethodGet, apiURL+"/company/12345678", httpmock.NewStringResponder(http.StatusOK, companyDetailsResponse)) 51 52 resp, err := GetCompanyName("12345678", r) 53 54 So(err, ShouldBeNil) 55 So(resp, ShouldEqual, "Test Company") 56 }) 57 }) 58 }