Skip to content

Commit d8d2e54

Browse files
authored
Merge pull request #3 from go-waitfor/copilot/fix-e7d31e01-8ba8-4ade-b2c7-5a367e8582a2
Improve README.md documentation with comprehensive guide and examples
2 parents f6c9596 + 2e2de11 commit d8d2e54

1 file changed

Lines changed: 139 additions & 4 deletions

File tree

README.md

Lines changed: 139 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
# waitfor-http
2-
HTTP resource readiness assertion library
32

4-
# Quick start
3+
[![Build](https://github.com/go-waitfor/waitfor-http/actions/workflows/build.yml/badge.svg)](https://github.com/go-waitfor/waitfor-http/actions/workflows/build.yml)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/go-waitfor/waitfor-http)](https://goreportcard.com/report/github.com/go-waitfor/waitfor-http)
5+
[![codecov](https://codecov.io/gh/go-waitfor/waitfor-http/branch/main/graph/badge.svg)](https://codecov.io/gh/go-waitfor/waitfor-http)
6+
7+
HTTP/HTTPS resource readiness assertion library for the [waitfor](https://github.com/go-waitfor/waitfor) framework.
8+
9+
## Overview
10+
11+
`waitfor-http` is a plugin for the [waitfor](https://github.com/go-waitfor/waitfor) library that provides HTTP and HTTPS resource testing capabilities. It allows you to wait for web services, APIs, and other HTTP endpoints to become available before proceeding with your application logic.
12+
13+
The library performs GET requests to specified URLs and considers the resource ready when it receives a successful HTTP response (status codes 200-399). This is particularly useful for:
14+
15+
- Waiting for web services to start up in Docker containers
16+
- Ensuring API dependencies are available before starting your application
17+
- Health checks in microservice architectures
18+
- Integration testing scenarios
19+
20+
## Installation
21+
22+
```bash
23+
go get github.com/go-waitfor/waitfor-http
24+
```
25+
26+
## Quick Start
527

628
```go
729
package main
@@ -19,7 +41,7 @@ func main() {
1941

2042
err := runner.Test(
2143
context.Background(),
22-
[]string{"http://locahost:5432", "https://www.google.com"},
44+
[]string{"http://localhost:8080", "https://api.example.com"},
2345
waitfor.WithAttempts(5),
2446
)
2547

@@ -28,4 +50,117 @@ func main() {
2850
os.Exit(1)
2951
}
3052
}
31-
```
53+
```
54+
55+
## Usage Examples
56+
57+
### Basic HTTP Testing
58+
59+
```go
60+
import (
61+
"context"
62+
"github.com/go-waitfor/waitfor"
63+
"github.com/go-waitfor/waitfor-http"
64+
)
65+
66+
func waitForService() error {
67+
runner := waitfor.New(http.Use())
68+
69+
ctx := context.Background()
70+
return runner.Test(ctx, []string{"http://localhost:3000"})
71+
}
72+
```
73+
74+
### Testing Multiple Endpoints
75+
76+
```go
77+
func waitForMultipleServices() error {
78+
runner := waitfor.New(http.Use())
79+
80+
endpoints := []string{
81+
"http://localhost:8080/health",
82+
"https://api.service.com/status",
83+
"http://database:5432",
84+
}
85+
86+
ctx := context.Background()
87+
return runner.Test(ctx, endpoints, waitfor.WithAttempts(10))
88+
}
89+
```
90+
91+
### With Timeout and Custom Configuration
92+
93+
```go
94+
import (
95+
"context"
96+
"time"
97+
"github.com/go-waitfor/waitfor"
98+
"github.com/go-waitfor/waitfor-http"
99+
)
100+
101+
func waitWithTimeout() error {
102+
runner := waitfor.New(http.Use())
103+
104+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
105+
defer cancel()
106+
107+
return runner.Test(
108+
ctx,
109+
[]string{"http://slow-starting-service:8080"},
110+
waitfor.WithAttempts(15),
111+
waitfor.WithInterval(2*time.Second),
112+
)
113+
}
114+
```
115+
116+
## API Reference
117+
118+
### `http.Use() waitfor.ResourceConfig`
119+
120+
Returns a resource configuration for HTTP/HTTPS testing that can be used with the waitfor framework.
121+
122+
### Supported URL Schemes
123+
124+
- `http://` - HTTP endpoints
125+
- `https://` - HTTPS endpoints
126+
127+
### Status Code Handling
128+
129+
The library considers the following HTTP status codes as successful:
130+
- **2xx Success**: 200-299 (OK, Created, Accepted, etc.)
131+
- **3xx Redirection**: 300-399 (Moved Permanently, Found, etc.)
132+
133+
Status codes 400 and above (4xx Client Error, 5xx Server Error) are considered failures.
134+
135+
## Error Handling
136+
137+
The library will return errors in the following cases:
138+
139+
- **Network errors**: Connection refused, timeout, DNS resolution failures
140+
- **HTTP errors**: 4xx and 5xx status codes
141+
- **Invalid URLs**: Malformed or nil URLs
142+
143+
Example error handling:
144+
145+
```go
146+
runner := waitfor.New(http.Use())
147+
err := runner.Test(ctx, []string{"http://localhost:8080"})
148+
149+
if err != nil {
150+
// Handle different types of errors
151+
fmt.Printf("Service not ready: %v\n", err)
152+
// Implement retry logic or fail gracefully
153+
}
154+
```
155+
156+
## Integration with waitfor
157+
158+
This library is designed to work with the [waitfor](https://github.com/go-waitfor/waitfor) framework. For more advanced configuration options like custom retry intervals, backoff strategies, and attempt limits, refer to the [waitfor documentation](https://github.com/go-waitfor/waitfor).
159+
160+
## Contributing
161+
162+
Contributions are welcome! Please feel free to submit a Pull Request.
163+
164+
## License
165+
166+
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)