<commit_before># vue-resource-mocker
Provide mock responses from calls to vue-resource.

# Installation

Add the beta path to your package.json
```json
"devDependencies": {
    "vue-resource-mocker": "thatsus/vue-resource-mocker#beta"
}
```

Install
```bash
npm install
```

# Usage

Import VueResourceMocker and instantiate a mocker for Vue.
```js
import Vue from 'vue';
import VueResourceMocker from 'vue-resource-mocker';

Vue.httpMocker = new VueResourceMocker();
Vue.use(Vue.httpMocker);
```

For each test, set the routes that you need.
```js
Vue.httpMocker.setRoutes({
    GET: {
        '/api/users/{id}': function (request) {
            var user = {
                id: 1,
                name: '<NAME>',
            };
            return request.respondWith(user, {status: 200});
        }
    }
});

Vue.http.get('/api/users/1')
    .then(response => {
        console.log(response.data.name); // => <NAME>
    });
```

# Routes

The `setRoutes` method accepts an object.

The object should have keys equal to the capitalized form of any verbs your test will need via Vue.http.

Example:
```js
Vue.httpMocker.setRoutes({
    GET: ...,
    POST: ...,
    PATCH: ...
});
```

The values of these verb keys are either arrays or objects.

## Object form

In the object form the keys are URL paths and the values are functions.

Whenever an HTTP request is made the request path is used to match against the URL paths in the routes.

Each function receives the request and should return a response built by calling `request.respondWith()`.

```js
{
    GET: {
        '/api/users', function (request) {
            return request.respondWith([
                {
                    id: 1,
                    name: '<NAME>'
                },
                {
                    id: 2,
                    name: '<NAME>'
                }
            ], {status: 200});
        }
    }
}
```

If the route path string contains curly-braced portions then those portions are wildcards.

The matching portions of the request path are sent as additional parameters to the function after being decoded from URL form.

If the request path includes a query string it is ignored while matching.

If more than one route matches, the first one is used.

## Array form

In the array form, each element is an object with the keys `route` and `use`. 

Example:

```js
{
    GET: [
        route: '/api/users', 
        use: function (request) {
            return request.respondWith([
                {
                    id: 1,
                    name: '<NAME>'
                },
                {
                    id: 2,
                    name: '<NAME>'
                }
            ], {status: 200});
        }
    ]
}
```

`route` is either a path string or a RegExp object. 

`use` is a function. 

Whenever an HTTP request is made the request path is used to match against the routes.

The `use` function receives the request and should return a response built by calling `request.respondWith()`.

If `route` is a RegExp with parenthesized parts or a string with curly-braced wildcards, the matching portions of the request path are sent as additional parameters to the function after being decoded from URL form.

If the request path includes a query string it is ignored while matching.

If more than one route matches, the first one is used.

# Errors

If the response has a 4xx, 5xx, or 0 status, the Promise returned by Vue.http will be rejected, causing the next catch closure to run.

If no route matches, the Promise is rejected with a 404 File Not Found response.

If a closure throws an error, the Promise is rejected with a 500 response and the thrown value is in the `data` key.

# Troubleshooting

If the response does not have a status given, the status will be 0. This will be an error state.<commit_msg>Explain new options<commit_after># vue-resource-mocker
Provide mock responses from calls to vue-resource.

# Installation

Add the beta path to your package.json
```json
"devDependencies": {
    "vue-resource-mocker": "thatsus/vue-resource-mocker#beta"
}
```

Install
```bash
npm install
```

# Usage

Import VueResourceMocker and VueResource, and instantiate a mocker for Vue.
```js
import Vue from 'vue';
import VueResource from 'vue-resource';
import VueResourceMocker from 'vue-resource-mocker';

Vue.httpMocker = new VueResourceMocker();
Vue.use(Vue.httpMocker);
```

For each test, set the routes that you need.
```js
Vue.httpMocker.setRoutes({
    GET: {
        '/api/users/{id}': function (request) {
            var user = {
                id: 1,
                name: '<NAME>',
            };
            return user;
        }
    }
});

Vue.http.get('/api/users/1')
    .then(response => {
        console.log(response.data.name); // => <NAME>
    });
```

# Routes

The `setRoutes` method accepts an object.

The object should have keys equal to the capitalized form of any verbs your test will need via Vue.http.

Example:
```js
Vue.httpMocker.setRoutes({
    GET: ...,
    POST: ...,
    PATCH: ...
});
```

The values of these verb keys are either arrays or objects.

## Object form

In the object form the keys are URL paths and the values are functions.

Whenever an HTTP request is made the request path is used to match against the URL paths in the routes.

Each function receives the request and may return data to respond to the request. Any value can be returned. For advanced options, return a Response object by calling `request.respondWith()`.

```js
{
    GET: {
        '/api/users', function (request) {
            return [
                {
                    id: 1,
                    name: '<NAME>'
                },
                {
                    id: 2,
                    name: '<NAME>'
                }
            ]);
        }
    }
}
```

If the route path string contains curly-braced portions then those portions are wildcards.

The matching portions of the request path are sent as additional parameters to the function after being decoded from URL form.

If the request path includes a query string it is ignored while matching.

If more than one route matches, the first one is used.

## Array form

In the array form, each element is an object with the keys `route` and `use`. 

Example:

```js
{
    GET: [
        route: '/api/users', 
        use: function (request) {
            return [
                {
                    id: 1,
                    name: '<NAME>'
                },
                {
                    id: 2,
                    name: '<NAME>'
                }
            ];
        }
    ]
}
```

`route` is either a path string or a RegExp object. 

`use` is a function. 

Whenever an HTTP request is made the request path is used to match against the routes.

The `use` function receives the request and may return data to respond to the request. Any value can be returned. For advanced options, return a Response object by calling `request.respondWith()`.

If `route` is a RegExp with parenthesized parts or a string with curly-braced wildcards, the matching portions of the request path are sent as additional parameters to the function after being decoded from URL form.

If the request path includes a query string it is ignored while matching.

If more than one route matches, the first one is used.

# Responses

The return value of a route closure can be any data type. The status will be 200 for regular responses.

For advanced responses, use request.respondWith(body, options). The `body` can be any data type. The `options` is an object with some of the following keys:

 * status - number, required
 * statusText - string
 * url - string
 * headers - object

The `headers` object can have any keys. Its values should be arrays.

# Errors

If the response has a 4xx, 5xx, or 0 status, the Promise returned by Vue.http will be rejected, causing the next catch closure to run.

If no route matches, the Promise is rejected with a 404 File Not Found response.

If a closure throws an error, the Promise is rejected with a 500 response and the thrown value is in the `data` key.
{'fraction_non_alphanumeric': 0.0864578194375925, 'fraction_numerical': 0.0053034040453872715, 'mean_word_length': 2.3342927631578947, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '691179', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2169, 'n_words': 1014}