Skip to content

Commit

Permalink
Add CI pipeline (#4)
Browse files Browse the repository at this point in the history
## Description
This PR adds a minimal CI configuration for the repo to enable before
merge checks.

## Related Issue
Closes #1
  • Loading branch information
martin-e91 authored Feb 24, 2024
1 parent a943dff commit eebf126
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: PR_checks

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2

- name: Build
run: swift build -v

- name: Run unit tests
run: swift test -v
7 changes: 4 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// swift-tools-version: 5.9
// swift-tools-version: 5.7.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Network",
platforms: [.iOS(.v13)],
platforms: [.iOS(.v13), .macOS(.v10_15)],
products: [
.library(name: "NetworkAPI", targets: ["NetworkAPI"]),
.library(name: "Network", targets: ["Network"]),
Expand All @@ -16,5 +16,6 @@ let package = Package(
.target(name: "Network", dependencies: ["NetworkAPI"]),
.target(name: "NetworkMocks", dependencies: ["NetworkAPI"]),
.testTarget(name: "NetworkTests", dependencies: ["Network", "NetworkMocks"])
]
],
swiftLanguageVersions: [.v5]
)
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The package has been designed with modularisation in mind. For this reason its c
Ideally you'd import the `Network` module in your dependency injection layer, extracting a `NetworkClient` instance from exposed factory `NetworkClientFactoryImpl`, like so

```swift

struct AppDependencies {
let networkClient: NetworkClient

Expand All @@ -75,16 +76,19 @@ After that, let's say you want to hit the PUNK API to retrieve a list of beers.
You'll want to declare a concrete `Endpoint`

```swift

struct PunkBeersEndpoint: Endpoint {
var scheme: String { "https" }
var host: String { "api.punkapi.com" }
var path: PathComponents { ["v2"] }
}

```

a concrete `NetworkRequest`

```swift

struct GetBeersRequest: NetworkRequest {
typealias Response = [Beer]

Expand All @@ -94,6 +98,7 @@ struct GetBeersRequest: NetworkRequest {

var endpoint: Endpoint { PunkBeersEndpoint() }
}

```

and finally you'll be able to hit the network with just 1 line of code
Expand Down
2 changes: 1 addition & 1 deletion Sources/Network/Core/NetworkClientImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public struct NetworkClientImpl: NetworkClient {
let session: URLSessionProtocol
let requestAdapter: NetworkRequestAdapter

package init(session: URLSessionProtocol, requestAdapter: NetworkRequestAdapter) {
init(session: URLSessionProtocol, requestAdapter: NetworkRequestAdapter) {
self.session = session
self.requestAdapter = requestAdapter
}
Expand Down
21 changes: 16 additions & 5 deletions Tests/NetworkTests/Core/NetworkRequestAdapterImplTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ final class NetworkRequestAdapterImplTests: XCTestCase {

func testURLRequest_whenEndpointHasValidURLComponents_shallReturnExpectedInstance() throws {
// Given
let validEndpoint = EndpointMock(scheme: "https", host: "google.com", path: ["hola", "barcelona"])
let validEndpoint = EndpointMock(scheme: "http", host: "google.com", path: ["hola", "barcelona"])
let request = NetworkRequestMock(method: .get, endpoint: validEndpoint, parameters: nil, responseMock: "")

// When
let result = try XCTUnwrap(sut.urlRequest(for: request))

// Then
XCTAssertEqual(result.httpMethod, "GET")
let expectedURL = URL(string: "https://google.com/hola/barcelona")!
XCTAssertEqual(result.url, expectedURL)
XCTAssertNil(result.httpBody)
let resultURLComponents = try XCTUnwrap(result.url.flatMap { URLComponents(url: $0, resolvingAgainstBaseURL: false) })
XCTAssertEqual(resultURLComponents.scheme, "http")
XCTAssertEqual(resultURLComponents.host, "google.com")
XCTAssertEqual(resultURLComponents.path, "/hola/barcelona")
XCTAssertNil(resultURLComponents.query)
}

func testURLRequest_whenEndpointHasInvalidScheme_shallThrowInvalidSchemeEndpointError() throws {
Expand Down Expand Up @@ -58,7 +62,14 @@ final class NetworkRequestAdapterImplTests: XCTestCase {

// Then
XCTAssertEqual(result.httpMethod, "GET")
let expectedURL = URL(string: "https://good-beers.com/api/v2?page=5&type=IPA")!
XCTAssertEqual(result.url, expectedURL)
XCTAssertNil(result.httpBody)
let resultURLComponents = try XCTUnwrap(result.url.flatMap { URLComponents(url: $0, resolvingAgainstBaseURL: false) })
XCTAssertEqual(resultURLComponents.scheme, "https")
XCTAssertEqual(resultURLComponents.host, "good-beers.com")
XCTAssertEqual(resultURLComponents.path, "/api/v2")
let expectedQueryItems = [URLQueryItem(name: "type", value: "IPA"), URLQueryItem(name: "page", value: "5")]
let resultQueryItems = try XCTUnwrap(resultURLComponents.queryItems)
XCTAssertEqual(resultQueryItems.count, expectedQueryItems.count)
XCTAssertTrue(expectedQueryItems.allSatisfy { resultQueryItems.contains($0) })
}
}

0 comments on commit eebf126

Please sign in to comment.