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

Initial build #1

Merged
merged 12 commits into from
Dec 7, 2020
16 changes: 16 additions & 0 deletions .github/workflows/clojure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@ jobs:

steps:
- uses: actions/checkout@v1

- uses: actions/setup-java@v1
with:
java-version: '9'

- name: Cache maven
uses: actions/cache@v2
env:
cache-name: cache-maven
with:
path: ~/.m2
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/project.clj') }}
restore-keys: |
${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/project.clj') }}

- name: Install dependencies
run: lein deps

- name: Run Clojure tests
run: lein test
2 changes: 1 addition & 1 deletion .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
issue-message: 'Thank you for taking the time to let us know!'
pr-message: 'Congratulations! This is your first PR to {{template}}!'
pr-message: 'Congratulations! This is your first PR to faker!'
9 changes: 0 additions & 9 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ name: Labeler
on: [pull_request]

jobs:
label:

runs-on: ubuntu-latest

steps:
- uses: actions/labeler@v2
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"

analysis:
name: runner / misspell
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v1.0.0 / 2020 Dec 07

> This release includes all major functionality
1 change: 0 additions & 1 deletion CODEOWNERS

This file was deleted.

8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Please note we have a code of conduct, please follow it in all your interactions

## Pull Requests

1. Update the [CHANGELOG.md](https://github.com/Wall-Brew-Co/{{template}}/blob/master/CHANGELOG.md) with details of all changes.
2. If any changes impact the external interface or use of **{{template}}**, please update the [README](https://github.com/Wall-Brew-Co/{{template}}/blob/master/README.md) to reflect any relevant differences.
1. Update the [CHANGELOG.md](https://github.com/nnichols/faker/blob/master/CHANGELOG.md) with details of all changes.
2. If any changes impact the external interface or use of **faker**, please update the [README](https://github.com/nnichols/faker/blob/master/README.md) to reflect any relevant differences.
3. Update all version numbers to the new version that this Pull Request would represent.
The versioning scheme we use is [SemVer](http://semver.org/).

Expand All @@ -20,7 +20,7 @@ If you have questions which aren't in the documentation, open a ticket or submit

If you're looking for ways to contribute, try adding/fixing tests.
Additionally improving documentation or adding examples as you learn a new project can is an easy way to pitch in.
If you're looking to pitch in more extensively, check out the open [issues and feature requests](https://github.com/Wall-Brew-Co/{{template}}/issues) and take a crack at one of them.
If you're looking to pitch in more extensively, check out the open [issues and feature requests](https://github.com/nnichols/faker/issues) and take a crack at one of them.

## Code of Conduct

Expand All @@ -29,4 +29,4 @@ If you're looking to pitch in more extensively, check out the open [issues and f
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.

A full copy of our [code of conduct may be found here.](https://github.com/Wall-Brew-Co/{{template}}/blob/master/CODE_OF_CONDUCT.md)
A full copy of our [code of conduct may be found here.](https://github.com/nnichols/faker/blob/master/CODE_OF_CONDUCT.md)
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Wall Brew Co
Copyright (c) 2020 Nick A Nichols

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
126 changes: 124 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,127 @@
# faker

## UNDER CONSTRUCTION
<a href="https://icons8.com/icon/xbZcFKwb9J7z/fraud"><img src="resources/icons8-fraud.png"></a>
[![Dependencies Status](https://versions.deps.co/nnichols/faker/status.svg)](https://versions.deps.co/nnichols/faker)
[![cljdoc badge](https://cljdoc.org/badge/nnichols/faker)](https://cljdoc.org/d/nnichols/faker/CURRENT)
![Github Runner](https://github.com/nnichols/faker/workflows/Clojure%20CI/badge.svg)

Check back later for updates!
A Clojure library for generating fake HTTP requests for [Ring](https://github.com/ring-clojure/ring).

> Faker is a negative word, but it's still cool.
> - Lee "Faker" Sang-hyeok from [*SK Telecom T1*](https://en.wikipedia.org/wiki/Faker_(gamer))

## Installation

A deployed copy of the most recent version of [nature can be found on clojars.](https://clojars.org/nnichols/faker)
To use it, add the following as a dependency in your `project.clj` file:

[![Clojars Project](http://clojars.org/nnichols/faker/latest-version.svg)](http://clojars.org/nnichols/faker)

The next time you build your application, [Leiningen](https://leiningen.org/) should pull it automatically.
Alternatively, you may clone or fork the repository to work with it directly.

## Usage

All functions necessary to generate requests are provided in the core namespace.
To create a fake request, simply specify the HTTP method and resource you'd like to access:

```clojure
(:require [faker.core :as faker])

(def get-request
(faker/mock-request :get "/v1/resource"))

;; => {:protocol "HTTP/1.1",
;; :server-port 8080,
;; :server-name "localhost",
;; :remote-addr "127.0.0.1",
;; :uri "/v1/resource",
;; :scheme :http,
;; :request-method :get,
;; :headers {"host" "localhost:8080"}}
```

This request is ready to pass directly to your Ring server for testing.
Since your application's middleware and routes are highly configurable, the `mock-request` function also accepts an optional map to customize the request.
The following options are supported:

- `protocol`: The HTTP protocol version to use. Defaults to HTTP/1.1
- `scheme`: The URI request scheme to use. Defaults to :http
- `hostname`: The URI request target hostname. Defaults to localhost
- `port`: The URI request port number. If the scheme is :https, default is set to 443. Otherwise, 8080
- `custom-route?`: A flag to determine if `resource` should be treated as a fully qualified route. Defaults to false, meaning the URI will be constructed from the `scheme`, `hostname`, `port`, and `resource`
- `headers`: A map of HTTP headers to add onto the request.
- `cookies`: A map of HTTP cookies to add onto the request.
- `content-type`: The HTTP content type the request should be sent with. Defaults to `application/json` for `body-type` :json, `application/xml` for `body-type` :xml, `application/x-www-form-urlencoded` for `body-type` :form. For `body-type` inferred, rely on content-type detection in `ring-mock`
- `content-length`: The HTTP content length in bytes for the request. If `body-type` != :raw, `ring-mock` will calculate this value for you.
- `body-type`: The keyword type of content in the request body, which is used to determine how to serialize EDN into the appropriate content-type and to set the `content-type` Expects one of :raw, :xml, :json, :form, :inferred. For :raw, assoc the `body` onto the request as is For :json, serialize the `body` with `cheshire` and set `content-type` to `application/json` For :xml, serialize the `body` with `clojure.data.xml` and set `content-type` to `application/xml` For :form, serialize the `body` as with `ring-mock` and set `content-type` to `application/x-www-form-urlencoded` For :inferred, use the automatic parsing in `ring-mock` and have it set the appropriate `content-type` Defaults to :inferred
- `body`: The body of the request to serialize according to `body-type`
- `query-parameters`: A map of query parameters to encode and add onto the request.

For example

```clojure
(faker/mock-request :get "/" {:headers {"auth" "my-secure-token"}
:body-type :json
:body {:key 123}
:scheme :https})

;; => {:protocol "HTTP/1.1",
;; :remote-addr "127.0.0.1",
;; :headers {"host" "localhost:443", "content-type" "application/json", "content-length" "11", "auth" "my-secure-token"},
;; :server-port 443,
;; :content-length 11,
;; :content-type "application/json",
;; :uri "/",
;; :server-name "localhost",
;; :body #object[java.io.ByteArrayInputStream 0x1f314cee "java.io.ByteArrayInputStream@1f314cee"],
;; :scheme :https,
;; :request-method :get}
```

Since most applications will use [standard HTTP methods,](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) several aliased versions exist.
These functions support the exact same options as `mock-request`

```clojure
(faker/get "/" {:headers {"auth" "my-secure-token"}
:body-type :json
:body {:key 123}
:scheme :https})

;; => {:protocol "HTTP/1.1",
;; :remote-addr "127.0.0.1",
;; :headers {"host" "localhost:443", "content-type" "application/json", "content-length" "11", "auth" "my-secure-token"},
;; :server-port 443,
;; :content-length 11,
;; :content-type "application/json",
;; :uri "/",
;; :server-name "localhost",
;; :body #object[java.io.ByteArrayInputStream 0x1f314cee "java.io.ByteArrayInputStream@1f314cee"],
;; :scheme :https,
;; :request-method :get}
```

Currently supported methods are:

- GET
- HEAD
- PUT
- DELETE
- CONNECT
- OPTIONS
- TRACE
- PATCH

Each of these HTTP methods corresponds 1:1 with a lowercase function name in `faker.core`

This library currently _does not_ attempt to coerce requests or enforce HTTP standards and best practices on generated requests.
For example, you may create a GET request with a body.
This can be helpful when writing middleware/handlers for malformed or malicious requests.

## Licensing

Copyright © 2020 [Nick Nichols](https://nnichols.github.io/)

Distributed under the [MIT License](https://github.com/nnichols/faker/blob/master/LICENSE)

[Fraud Icon by Icons8](https://icons8.com/icon/xbZcFKwb9J7z/fraud)
2 changes: 1 addition & 1 deletion bin/verchg
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function bump {
# and populate the following variables:
#
# $version = "1.54.3"
# $project = "{{template}}"
# $project = "faker"
# $today = "2019 Dec 12"
#
function pull_values {
Expand Down
17 changes: 12 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
(defproject com.wallbrew/{{template}} "1.0.0"
:description "TODO: Fill me in!"
:url "https://github.com/Wall-Brew-Co/{{template}}"
(defproject nnichols/faker "1.0.0"
:description "A clojure library for creating fake requests"
:url "https://github.com/nnichols/faker"
:license {:name "MIT"
:url "https://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.10.0"]]
:profiles {:uberjar {:aot :all}}
:dependencies [[org.clojure/data.xml "0.2.0-alpha6"]
[org.clojure/clojure "1.10.0"]
[ring/ring-mock "0.4.0"]]
:profiles {:uberjar {:aot :all}
:dev {:dependencies [[circleci/bond "0.4.0"]
[cheshire "5.10.0"]
[compojure "1.6.2"]
[ring/ring-core "1.8.2"]
[ring/ring-json "0.5.0"]]}}
:min-lein-version "2.5.3")
Binary file added resources/icons8-fraud.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading